Skip to content

Modularizing the schema #5

@raphox

Description

@raphox

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions