I have code-first
declarations on project. But I want to use them as microservices. In this case, how can i separate my typeDefs
and resolvers
according to Apollo's schema-first
approach. Is it possible ?. I didn't find any resources that contain this approach in graphql-js
and apollo
docs. It would be great any advice ..
In Apollo
docs it described as (can be access here):
const server = new ApolloServer({ schema: buildSubgraphSchema([{ typeDefs, resolvers }]) });
buildSubgraphSchema()
doesn't support schema as argument.
Wanted approach:
const server = new ApolloServer({
// Instead of schema-first style
// schema : buildSubgraphSchema([{ typeDefs, resolvers }])
// this:
schema: buildSubgraphSchema([userSchema])
});
Take a look at the code :
userType.ts
const userType = new GraphQLObjectType({
name: 'User',
description: 'User Type Definition',
fields: (): any => ({
username: {
type: new GraphQLNonNull(GraphQLString),
},
email: {
type: GraphQLString,
},
phone: {
type: GraphQLString,
},
firstName: {
type: GraphQLString,
},
lastName: {
type: GraphQLString,
},
}),
});
userQueries.ts
const userQueries = {
users: {
type: new GraphQLList(userType),
description: 'Return users',
resolve: () => {
return getUsers();
},
},
};
query.ts
const query = new GraphQLObjectType({
name: 'Query',
description: 'Queries',
fields: userQueries,
});
schema.ts
const userSchema = new GraphQLSchema({
query,
types: [userType],
});
server.ts
const server = new ApolloServer({ userSchema });
server.listen(4000).then(({ url }) => {
console.log('Running a GraphQL API server at localhost:4000/graphql');
});