9.0.11 • Published 7 months ago

graphql-compose v9.0.11

Weekly downloads
444,331
License
MIT
Repository
github
Last release
7 months ago

graphql-compose

npm.io 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

@directus-asolole/apirentalainen-apigraphql-authentification-serviceinvoicewave_back@jakeblaxon-graphql-mesh/grpc@jakeblaxon-graphql-mesh/json-schema@jakeblaxon-graphql-mesh/mongoose@jakeblaxon-graphql-mesh/mysql@jakeblaxon-graphql-mesh/odata@iharob/graphql-mesh-odata@iharob/graphql-mesh-grpc@iharob/graphql-mesh-json-schema@iharob/graphql-mesh-mongoose@iharob/graphql-mesh-mysql@devticon/offsura@reldb/runkf-personagraphql-cmsgit-cms-remarkdirectus9-literel-server@ldsgy/schema-graphqldimensio@sayjava/scaffold-schema@sayjava/sgraph-core@everything-registry/sub-chunk-1784synqueitwind-ssgworldql-coresgnm-neo4j-graphqlshapeable-starter-websamba-bo-apistarfleet-commandrsjs-server-graphqlsecurity-catalogsthehunt-paymentgridsome-makegridsomegraphql-compose-mysqlgraphql-mongoose-schemabuildergraphql-sequelize-toolsgraphql-mesh-carottegraphticon-schemajamplay-mongoose-elasticinvolves-changelog-serverirajs-graphqlmongoose-graphql-serverjunglejskomahasura-qb-codegenhyperdrive-daemon-client-graphqlimgix-gatsby-forkgraffitigraphql-auth-servicekaptuer-with-graphqlparkydbopenneuro-serveroffsuranextql_schemaneogoosemv-data-coremv-data-core-alpha-buildquantum_directus_apiragate-clitreeorm@anthor/microservices-core@anthor/core-graphql@ascorbic/gatsby@arc-directus/api@atls/nestjs-gateway@clairview/api@2109-t5/server@aclisp/directus@7hci/gatsby-pge@absa-subatomic/occurrence-client@bonobolabs/gatsby@db-studio/api@digicube/apixr3nginexrenginewapplr-graphql@financial-times/tc-schema-sdk@financial-times/biz-ops-graphql-tools@gymlib/codegen-swap-directive@foundationjs/persistence@flatbread/core@hooleyandburch/gridsome@gitaflow/api@infinitebrahmanuniverse/nolb-graphql-c@krypton-org/krypton-auth@kriya/gridsome@litewarp/graphql@lorefnon/garph@macko911/gatsby@mathix420/graphql@marvi-io/core@netlify/content-engine@new-mv-datacore/api@neo4j/graphql@dylanvann/gatsby
9.0.11

7 months ago

9.0.10

2 years ago

9.0.9

2 years ago

9.0.8

3 years ago

9.0.7

3 years ago

9.0.6

3 years ago

9.0.5

3 years ago

9.0.4

3 years ago

9.0.3

3 years ago

9.0.2

3 years ago

9.0.1

3 years ago

9.0.0

3 years ago

8.0.0-alpha.1

4 years ago

8.0.0-alpha.2

4 years ago

8.1.0

4 years ago

8.0.1

4 years ago

8.0.0

4 years ago

7.25.1

4 years ago

7.25.0

4 years ago

7.24.4

4 years ago

7.24.3

4 years ago

7.24.2

4 years ago

7.24.1

4 years ago

7.24.0

4 years ago

7.23.0

4 years ago

7.22.1

4 years ago

7.22.0

4 years ago

7.21.5

4 years ago

7.21.4

4 years ago

7.21.3

4 years ago

7.21.2

4 years ago

7.21.1

4 years ago

7.21.0

4 years ago

7.20.1

4 years ago

7.20.0

4 years ago

7.19.5

4 years ago

7.19.4

4 years ago

7.19.3

4 years ago

7.19.2

4 years ago

7.19.1

4 years ago

7.19.0

4 years ago

7.18.1

4 years ago

7.18.0

4 years ago

7.17.0

4 years ago

7.16.1

5 years ago

7.16.0

5 years ago

7.15.0

5 years ago

7.14.4

5 years ago

7.14.3

5 years ago

7.14.2

5 years ago

7.14.1

5 years ago

7.14.0

5 years ago

7.13.1

5 years ago

7.13.0

5 years ago

7.12.5

5 years ago

7.12.4

5 years ago

7.12.2

5 years ago

7.12.3

5 years ago

7.12.1

5 years ago

7.12.0

5 years ago

7.11.0

5 years ago

7.10.1

5 years ago

7.10.0

5 years ago

7.9.0

5 years ago

7.7.0

5 years ago

7.8.0

5 years ago

6.3.8

5 years ago

7.6.4

5 years ago

7.6.3

5 years ago

7.6.2

5 years ago

7.6.1

5 years ago

7.6.0

5 years ago

7.5.0

5 years ago

6.3.7

5 years ago

7.4.3

5 years ago

7.4.2

5 years ago

7.4.1

5 years ago

6.3.6

5 years ago

7.4.0

5 years ago

7.3.0

5 years ago

6.3.5

5 years ago

6.3.4

5 years ago

7.2.0

5 years ago

6.3.3

5 years ago

7.1.0

5 years ago

7.0.4

5 years ago

7.0.3

5 years ago

7.0.2

6 years ago

7.0.1

6 years ago

7.0.0

6 years ago

6.3.2

6 years ago

6.3.1

6 years ago

6.3.0

6 years ago

6.2.0

6 years ago

6.1.4

6 years ago

6.1.3

6 years ago

6.1.2

6 years ago

6.1.1

6 years ago

6.1.0

6 years ago

6.0.3

6 years ago

6.0.2

6 years ago

6.0.1

6 years ago

6.0.0

6 years ago

5.12.0

6 years ago

5.11.0

6 years ago

5.10.4

6 years ago

5.10.3

6 years ago

5.10.2

6 years ago

5.10.1

6 years ago

5.10.0

6 years ago

5.9.1

6 years ago

5.9.0

6 years ago

5.8.1

6 years ago

5.8.0

6 years ago

5.7.0

6 years ago

5.6.0

6 years ago

5.5.0

6 years ago

5.4.1

6 years ago

5.4.0

6 years ago

5.3.4

6 years ago

5.3.3

6 years ago

5.3.2

6 years ago

5.3.1

6 years ago

5.3.0

6 years ago

5.2.0

6 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.9.0

6 years ago

4.8.2

6 years ago

4.8.1

6 years ago

4.8.0

6 years ago

4.7.1

6 years ago

4.7.0

6 years ago

4.6.0

6 years ago

4.5.0

6 years ago

4.4.1

6 years ago

4.4.0

6 years ago

4.3.1

6 years ago

4.3.0

7 years ago

4.2.0

7 years ago

4.1.0

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.6

7 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

3.0.0-beta.6

7 years ago

3.0.0-beta.5

7 years ago

3.0.0-beta.4

7 years ago

3.0.0-beta.3

7 years ago

3.0.0-beta.2

7 years ago

3.0.0-beta.1

7 years ago

2.13.1

7 years ago

2.13.0

7 years ago

2.12.0

7 years ago

2.11.0

7 years ago

2.10.1

7 years ago

2.10.0

7 years ago

2.9.6

7 years ago

2.9.5

7 years ago

2.9.4

7 years ago

2.9.3

7 years ago

2.9.2

7 years ago

2.9.1

7 years ago

2.9.0

7 years ago

2.8.1

7 years ago

2.8.0

7 years ago

2.7.0

7 years ago

2.6.1

7 years ago

2.6.0

7 years ago

2.5.0

7 years ago

2.4.0

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.0

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

2.0.0-alpha.3

7 years ago

2.0.0-alpha.2

7 years ago

2.0.0-alpha.1

7 years ago

1.21.1

7 years ago

1.21.0

7 years ago

1.20.4

7 years ago

1.20.3

7 years ago

1.20.2

7 years ago

1.20.1

7 years ago

1.20.0

7 years ago

1.19.4

7 years ago

1.19.3

7 years ago

1.19.2

7 years ago

1.19.1

7 years ago

1.19.0

7 years ago

1.18.1

8 years ago

1.18.0

8 years ago

1.17.3

8 years ago

1.17.2

8 years ago

1.17.1

8 years ago

1.17.0

8 years ago

1.16.0

8 years ago

1.15.0

8 years ago

1.14.2

8 years ago

1.14.1

8 years ago

1.14.0

8 years ago

1.13.0

8 years ago

1.12.1

8 years ago

1.12.0

8 years ago

1.11.0

8 years ago

1.10.1

8 years ago

1.10.0

8 years ago

1.9.0

8 years ago

1.8.1

8 years ago

1.8.0

8 years ago

1.7.1

8 years ago

1.7.0

8 years ago

1.6.0

8 years ago

1.5.1

8 years ago

1.5.0

8 years ago

1.4.4

8 years ago

1.4.3

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.25

8 years ago

0.0.24

8 years ago

0.0.23

8 years ago

0.0.22

8 years ago

0.0.21

8 years ago

0.0.20

8 years ago

0.0.19

8 years ago

0.0.18

8 years ago

0.0.17

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago