graphql-compose

GraphQL schema builder from different data sources with middleware extensions.

MIT 270 个版本
安装
npm install graphql-compose
yarn add graphql-compose
pnpm add graphql-compose
bun add graphql-compose
README

graphql-compose

codecov coverage Travis npm Commitizen friendly TypeScript compatible Backers on Open Collective Sponsors on Open Collective

graphql-compose – provides a type registry with a bunch of methods for programmatic schema construction. It allows not only to extend types but also remove fields, interfaces, args. If you want to write your graphql schema generator – graphql-compose is a good instrument for you.

  • provides methods for editing GraphQL output/input types (add/remove fields/args/interfaces)
  • introduces Resolver's – the named graphql fieldConfigs, which can be used for finding, updating, removing records
  • provides an easy way for creating relations between types via Resolver's
  • provides converter from OutputType to InputType
  • provides projection parser from AST
  • provides GraphQL schema language for defining simple types
  • adds additional types Date, Json

And a little bit more

graphql-compose-[plugin] – are declarative generators/plugins built on top of graphql-compose, which take some ORMs, schema definitions and create GraphQL Models from them or modify existing GraphQL Types.

Type generators built on top graphql-compose

Utility plugins:

Documentation

graphql-compose.github.io

Live Demos

Examples

Please follow Quick Start Guide for the complete example.

Here is just a demo of ambiguity ways of types definitions:

import { schemaComposer} from 'graphql-compose';

// You may use SDL format for type definition
const CityTC = schemaComposer.createObjectTC(`
  type City {
    code: String!
    name: String!
    population: Number
    countryCode: String
    tz: String
  }
`);

// Define type via Config object
const CountryTC = schemaComposer.createObjectTC({
  name: 'Country',
  fields: {
    title: 'String',
    geo: `type LonLat { lon: Float, lat: Float }`,
    hoisting: {
      type: () => AnotherTC,
      description: `
        You may wrap type in thunk for solving
        hoisting problems when two types cross reference
        each other.
      `,
    }
  }
});

// Or via declarative methods define some additional fields
CityTC.addFields({
  country: CountryTC, // some another Type
  ucName: { // standard GraphQL like field definition
    type: GraphQLString,
    resolve: (source) => source.name.toUpperCase(),
  },
  currentLocalTime: { // extended GraphQL Compose field definition
    type: 'Date',
    resolve: (source) => moment().tz(source.tz).format(),
    projection: { tz: true }, // load `tz` from database, when requested only `localTime` field
  },
  counter: 'Int', // shortening for only type definition for field
  complex: `type ComplexType {
    subField1: String
    subField2: Float
    subField3: Boolean
    subField4: ID
    subField5: JSON
    subField6: Date
  }`,
  list0: {
    type: '[String]',
    description: 'Array of strings',
  },
  list1: '[String]',
  list2: ['String'],
  list3: [new GraphQLOutputType(...)],
  list4: [`type Complex2Type { f1: Float, f2: Int }`],
});

// Add resolver method
CityTC.addResolver({
  kind: 'query',
  name: 'findMany',
  args: {
    filter: `input CityFilterInput {
      code: String!
    }`,
    limit: {
      type: 'Int',
      defaultValue: 20,
    },
    skip: 'Int',
    // ... other args if needed
  },
  type: [CityTC], // array of cities
  resolve: async ({ args, context }) => {
    return context.someCityDB
      .findMany(args.filter)
      .limit(args.limit)
      .skip(args.skip);
  },
});

// Remove `tz` field from schema
CityTC.removeField('tz');

// Add description to field
CityTC.extendField('name', {
  description: 'City name',
});

schemaComposer.Query.addFields({
  cities: CityTC.getResolver('findMany'),
  currentTime: {
    type: 'Date',
    resolve: () => Date.now(),
  },
});

schemaComposer.Mutation.addFields({
  createCity: CityTC.getResolver('createOne'),
  updateCity: CityTC.getResolver('updateById'),
  ...adminAccess({
    removeCity: CityTC.getResolver('removeById'),
  }),
});

function adminAccess(resolvers) {
  Object.keys(resolvers).forEach(k => {
    resolvers[k] = resolvers[k].wrapResolve(next => rp => {
      // rp = resolveParams = { source, args, context, info }
      if (!rp.context.isAdmin) {
        throw new Error('You should be admin, to have access to this action.');
      }
      return next(rp);
    });
  });
  return resolvers;
}

// construct schema which can be passed to express-graphql, apollo-server or graphql-yoga
export const schema = schemaComposer.buildSchema();

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

MIT

版本列表
9.1.0 2025-01-11
9.0.11 2024-05-11
9.0.10 2022-10-26
9.0.9 2022-09-05
9.0.8 2022-03-28
9.0.7 2022-02-10
9.0.6 2021-12-21
9.0.5 2021-12-16
9.0.4 2021-10-16
9.0.3 2021-09-07
9.0.2 2021-07-25
9.0.1 2021-06-02
9.0.0 2021-05-30
8.1.0 2021-05-11
8.0.1 2021-04-30
8.0.0-alpha.2 2021-04-29
8.0.0-alpha.1 2021-04-28
8.0.0 2021-04-30
7.25.1 2021-03-18
7.25.0 2021-02-11
7.24.4 2021-01-29
7.24.3 2021-01-27
7.24.2 2021-01-21
7.24.1 2021-01-06
7.24.0 2021-01-06
7.23.0 2020-11-22
7.22.1 2020-10-12
7.22.0 2020-10-11
7.21.5 2020-09-26
7.21.4 2020-09-23
7.21.3 2020-09-22
7.21.2 2020-09-21
7.21.1 2020-09-12
7.21.0 2020-09-11
7.20.1 2020-08-27
7.20.0 2020-08-26
7.19.5 2020-08-26
7.19.4 2020-08-10
7.19.3 2020-08-08
7.19.2 2020-08-08
7.19.1 2020-08-02
7.19.0 2020-08-02
7.18.1 2020-06-03
7.18.0 2020-06-01
7.17.0 2020-05-29
7.16.1 2020-05-22
7.16.0 2020-05-22
7.15.0 2020-05-10
7.14.4 2020-04-20
7.14.3 2020-04-20
7.14.2 2020-04-19
7.14.1 2020-04-05
7.14.0 2020-03-06
7.13.1 2020-03-06
7.13.0 2020-03-05
7.12.5 2020-03-03
7.12.4 2020-02-28
7.12.3 2020-02-22
7.12.2 2020-02-22
7.12.1 2020-02-20
7.12.0 2020-02-15
7.11.0 2020-02-06
7.10.1 2020-02-06
7.10.0 2020-02-05
7.9.0 2020-01-15
7.8.0 2019-12-27
7.7.0 2019-12-27
7.6.4 2019-12-19
7.6.3 2019-12-19
7.6.2 2019-12-11
7.6.1 2019-11-25
7.6.0 2019-11-22
7.5.0 2019-11-21
7.4.3 2019-11-04
7.4.2 2019-11-01
7.4.1 2019-11-01
7.4.0 2019-10-18
7.3.0 2019-07-23
7.2.0 2019-07-05
7.1.0 2019-06-04
7.0.4 2019-06-04
7.0.3 2019-05-26
7.0.2 2019-05-16
7.0.1 2019-04-29
7.0.0 2019-04-28
6.3.8 2019-12-19
6.3.7 2019-11-04
6.3.6 2019-10-22
6.3.5 2019-07-05
6.3.4 2019-07-05
6.3.2 2019-04-20
6.3.1 2019-04-17
6.3.0 2019-04-16
6.2.0 2019-04-08
6.1.4 2019-04-05
6.1.3 2019-04-04
6.1.2 2019-04-04
6.1.1 2019-04-01
6.1.0 2019-03-28
6.0.3 2019-03-17
6.0.2 2019-03-16
6.0.1 2019-03-15
6.0.0 2019-03-15
5.12.0 2019-03-11
5.11.0 2019-03-05
5.10.4 2019-02-11
5.10.3 2019-02-11
5.10.2 2019-02-11
5.10.1 2019-02-07
5.10.0 2019-02-06
5.9.1 2019-02-06
5.9.0 2019-02-05
5.8.1 2019-02-05
5.8.0 2019-02-04
5.7.0 2019-02-01
5.6.0 2019-01-31
5.5.0 2019-01-16
5.4.1 2019-01-10
5.4.0 2018-12-24
5.3.4 2018-11-11
5.3.3 2018-10-29
5.3.2 2018-10-24
5.3.1 2018-10-24
5.3.0 2018-10-24
5.2.0 2018-09-18
5.1.1 2018-09-10
5.1.0 2018-09-09
5.0.2 2018-09-05
5.0.1 2018-09-05
5.0.0 2018-09-05
4.8.2 2018-08-11
4.8.1 2018-07-31
4.8.0 2018-07-26
4.7.1 2018-07-16
4.7.0 2018-07-13
4.6.0 2018-07-12
4.5.0 2018-07-10
4.4.1 2018-06-26
4.4.0 2018-06-26
4.3.1 2018-06-12
4.3.0 2018-04-27
4.2.0 2018-04-25
4.1.0 2018-04-25
4.0.1 2018-04-19
4.0.0 2018-04-16
3.1.1 2018-04-03
3.1.0 2018-03-30
3.0.6 2018-02-22
3.0.5 2018-02-20
3.0.4 2018-02-15
3.0.3 2018-02-14
3.0.2 2018-02-14
3.0.1 2018-02-13
3.0.0-beta.6 2018-02-12
3.0.0-beta.5 2018-02-12
3.0.0-beta.4 2018-02-09
3.0.0-beta.3 2018-02-09
3.0.0-beta.2 2018-02-09
3.0.0-beta.1 2018-02-09
3.0.0 2018-02-12
2.13.1 2018-02-01
2.13.0 2018-02-01
2.12.0 2018-01-31
2.11.0 2018-01-31
2.10.1 2017-11-30
2.10.0 2017-11-30
2.9.6 2017-11-04
2.9.5 2017-11-01
2.9.4 2017-10-20
2.9.3 2017-10-20
2.9.2 2017-10-19
2.9.1 2017-10-06
2.9.0 2017-09-07
2.8.1 2017-09-06
2.8.0 2017-09-06
2.7.0 2017-09-06
2.6.1 2017-09-05
2.6.0 2017-09-05
2.5.0 2017-09-03
2.4.0 2017-08-30
2.3.2 2017-08-18
2.3.1 2017-08-15
2.3.0 2017-08-11
2.2.0 2017-08-01
2.1.3 2017-07-23
2.1.2 2017-07-23
2.1.1 2017-06-27
2.1.0 2017-06-24
2.0.0-alpha.3 2017-06-12
2.0.0-alpha.2 2017-06-12
2.0.0-alpha.1 2017-06-12
2.0.0 2017-06-21
1.21.1 2017-06-10
1.21.0 2017-06-09
1.20.4 2017-06-06
1.20.3 2017-06-06
1.20.2 2017-06-06
1.20.1 2017-06-05
1.20.0 2017-06-05
1.19.4 2017-06-05
1.19.3 2017-06-05
1.19.2 2017-06-04
1.19.1 2017-06-04
1.19.0 2017-06-03
1.18.1 2017-04-03
1.18.0 2017-03-21
1.17.3 2017-03-20
1.17.2 2017-03-16
1.17.1 2017-03-13
1.17.0 2017-03-13
1.16.0 2017-03-13
1.15.0 2017-03-10
1.14.2 2017-03-09
1.14.1 2017-03-09
1.14.0 2017-03-03
1.13.0 2017-02-28
1.12.1 2017-02-20
1.12.0 2017-01-27
1.11.0 2017-01-27
1.10.1 2017-01-25
1.10.0 2017-01-19
1.9.0 2016-12-28
1.8.1 2016-12-27
1.8.0 2016-12-21
1.7.1 2016-12-21
1.7.0 2016-12-15
1.6.0 2016-12-14
1.5.1 2016-12-13
1.5.0 2016-11-14
1.4.4 2016-11-08
1.4.3 2016-11-03
1.4.2 2016-10-31
1.4.1 2016-10-31
1.4.0 2016-10-25
1.3.0 2016-10-20
1.2.2 2016-10-14
1.2.1 2016-10-14
1.2.0 2016-10-13
1.1.1 2016-10-13
1.1.0 2016-10-13
1.0.0 2016-10-12
0.2.1 2016-10-10
0.2.0 2016-10-10
0.1.1 2016-10-07
0.1.0 2016-10-07
0.0.25 2016-10-04
0.0.24 2016-09-13
0.0.23 2016-09-06
0.0.22 2016-08-25
0.0.21 2016-08-25
0.0.20 2016-08-22
0.0.19 2016-08-19
0.0.18 2016-08-18
0.0.17 2016-08-15
0.0.16 2016-08-13
0.0.15 2016-08-10
0.0.14 2016-08-09
0.0.13 2016-08-08
0.0.12 2016-08-08
0.0.11 2016-07-28
0.0.10 2016-07-21
0.0.9 2016-07-20
0.0.8 2016-07-18
0.0.7 2016-07-18
0.0.6 2016-07-15
0.0.5 2016-07-08
0.0.4 2016-07-07
0.0.3 2016-07-03
0.0.2 2016-07-01
0.0.1 2016-06-07