Batuhan
Batuhan

Reputation: 1611

Has Apollo Federation support code-first approach?

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');
});

Upvotes: 0

Views: 549

Answers (1)

Batuhan
Batuhan

Reputation: 1611

I find the solution of described problem in graphql and apollo-server packages.

We can obtain typeDefs with gql and printSchema functions like this:

Note: Undefined objects and variables can be found on question.

import { gql } from 'apollo-server';
import { /* GraphQLSchema */ printSchema } from 'graphql';

const typeDefs = gql(
    printSchema(
      userSchema,
      // Or 
      //  new GraphQLSchema({
      //    query: queryType,
      //    mutation: mutationType,
      //  })
    )
);

const resolvers = {
  Query: userQueries,
  // Mutation: someMutationObject, if there is mutation.
};

const server = new ApolloServer({
  schema: buildSubgraphSchema([{ typeDefs, resolvers }]),
});

Important: Created schema with buildSubgraphSchema() has no support for subfield resolver. I don't know what is actual name is. But it seems like:

fullName: {
      type: GraphQLString,
      description: 'TESTSETSET',
      resolve: (
        parent: { firstName: any; lastName: any },
        args: any,
        context: any,
        info: any,
      ) => {
        console.log('Here is not working if buildSubgraphSchema() used.');

        return parent.firstName + parent.lastName;
      },
    },

Upvotes: 2

Related Questions