I have declarations using 'code-first' approach in my project, but now I want to utilize them as microservices. How can I separate my 'typeDefs' and 'resolvers' following Apollo's 'schema-first' methodology? Is this even possible? I couldn't find any resources that discuss this specific scenario in the documentation for 'graphql-js' and 'apollo'. Any advice would be greatly appreciated.
The Apollo
documentation mentions (accessible here):
const server = new ApolloServer({ schema: buildSubgraphSchema([{ typeDefs, resolvers }]) });
It seems that 'buildSubgraphSchema()' does not accept schema as an argument.
Desired approach:
const server = new ApolloServer({
// Moving away from schema-first style
// schema : buildSubgraphSchema([{ typeDefs, resolvers }])
// towards 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');
});