fullstack-graphql-apollo-react-meteor icon indicating copy to clipboard operation
fullstack-graphql-apollo-react-meteor copied to clipboard

Modularizing the schema

Open raphox opened this issue 6 years ago • 0 comments

Hi @LevelUpTuts, I found a good way to modularize the API codes. I suggest you use bundle from graphql-modules. Take a look at my code https://bitbucket.org/raphox/react-fullstack/src/master/imports/api/

The main code:

// resolutions.js
...
const schema = `
  type Resolution {
    _id: String
    name: String
    author: Author
  }
`;

export const queries = `
  resolutions: [Resolution]
  resolution(_id: String): Resolution
`;

export const mutations = `
  createResolution(name: String!): Resolution
  removeResolution(_id: String!): Resolution
`;

const resolvers = {
  queries: {
    resolutions() {
      ...
  },
  mutations: {
    createResolution(obj, { name }, context) {
      ...
  },
  Resolution: {
    author(resolution) {
      return Authors.findOne(resolution.author);
    }
  }
}

export default () => ({
  schema,
  queries,
  mutations,
  resolvers,
  modules: [authors]
});
// register-api.js
import resolutions from '../../api/resolutions';

const modules = [resolutions];
const schema = makeExecutableSchema(bundle(modules));

createApolloServer({
  schema
});

I still need to finalize the implementation, but I can already work with multiple queries, schemas and mutations.

References:

  • https://www.apollographql.com/docs/graphql-tools/generate-schema.html#modularizing
  • https://github.com/lucasconstantino/graphql-modules

raphox avatar Jan 20 '18 12:01 raphox