it('a query returns an empty array', async () => { const query = ` query { Post { results { id, title, body, published, post_date } } } `; const result = await graphql(schema, query); expect(result.data!.Post.results).to.deep.equal([]); });
async () => { const mergedSchema = mergeSchemas({ schemas: [ // pull in an executable schema just so mergeSchema doesn't complain // about not finding default types (e.g. ID) propertySchema, testSchemaWithInterfaceResolvers ], resolvers }); const query = `{ user { id name } }`; const response = await graphql(mergedSchema, query); expect(response.errors.length).to.equal(1); expect(response.errors[0].message).to.equal('Cannot return null for ' + 'non-nullable field User.id.'); expect(response.errors[0].path).to.deep.equal(['user', 'id']); });
it('does not return "results" and "meta" objects at lower levels', async () => { const query = ` query { Post { results { id, title, comments { id, comment } } meta { limit offset totalCount } } } `; const result = await graphql(schema, query); expect(result.data!.Post).to.deep.equal({ results: [ { id: 1, title: 'RevJS v1.0.0 Released!', comments: [ { id: 1, comment: 'I totally agree'}, { id: 2, comment: 'Sweet!' } ] }, { id: 2, title: 'JavaScript is Awesome', comments: [] }, { id: 3, title: 'Ruby Sucks', comments: [] } ], meta: { limit: 20, offset: 0, totalCount: 3 } }); });
it('I can get Posts by their id', async () => { const query = ` query { Post(where: {id : 2}) { results { id, title } } } `; const result = await graphql(schema, query); expect(result.data!.Post.results).to.deep.equal([{ id: 2, title: 'JavaScript is Awesome' }]); });
return new Observable(observer => { const { query, operationName, variables } = operation; const { graphqlContext } = operation.getContext(); graphql( schema, print(query), null, graphqlContext, variables, operationName, ) .then(result => { observer.next(result); observer.complete(); }) .catch(observer.error.bind(observer)); });
it('logs any Promise reject errors', done => { const shorthand = ` type RootQuery { just_a_field: Int } type RootMutation { species(name: String): String stuff: String } schema { query: RootQuery mutation: RootMutation } `; const resolve = { RootMutation: { species: () => { return new Promise((_, reject) => { reject(new Error('oops!')); }); }, stuff: () => { return new Promise((_, reject) => { reject(new Error('oh noes!')); }); }, }, }; const logger = new Logger(); const jsSchema = makeExecutableSchema({ typeDefs: shorthand, resolvers: resolve, logger, }); const testQuery = 'mutation { species, stuff }'; const expected0 = 'Error in resolver RootMutation.species\noops!'; const expected1 = 'Error in resolver RootMutation.stuff\noh noes!'; graphql(jsSchema, testQuery).then(() => { assert.equal(logger.errors.length, 2); assert.equal(logger.errors[0].message, expected0); assert.equal(logger.errors[1].message, expected1); done(); }); });
it('fragments on non-compatible sub schema types', async () => { const result = await graphql( mergedSchema, ` query($bid: ID!) { node(id: $bid) { __typename id ...PropertyFragment ...BookingFragment ...CustomerFragment } } fragment PropertyFragment on Property { name } fragment BookingFragment on Booking { startTime endTime } fragment CustomerFragment on Customer { name } `, {}, {}, { bid: 'b1', }, ); expect(result).to.deep.equal({ data: { node: { __typename: 'Booking', id: 'b1', startTime: '2016-05-04', endTime: '2016-06-03', }, }, }); });
it('should match nerdy snapshot', async () => { const query = ` query Q { jokes { byCategory(category: NERDY) { id text categories } } } `; const result = await graphql(schema, query, rootValue, context); const { data, errors } = result; expect({ data, errors }).toMatchSnapshot(); });
/** * Exports a PostGraphQL schema by looking at a Postgres client. */ export default async function exportPostGraphQLSchema ( schema: GraphQLSchema, options: { exportJsonSchemaPath?: string, exportGqlSchemaPath?: string, } = {}, ): Promise<void> { // JSON version if (typeof options.exportJsonSchemaPath === 'string') { const result = await graphql(schema, introspectionQuery) await writeFileAsync(options.exportJsonSchemaPath, JSON.stringify(result, null, 2)) } // Schema language version if (typeof options.exportGqlSchemaPath === 'string') { await writeFileAsync(options.exportGqlSchemaPath, printSchema(schema)) } }
it('Model methods are available in the schema', async () => { const query = ` query { __schema { mutationType { fields { name } } } } `; const result = await graphql(schema, query); expect(result.data!.__schema.mutationType.fields).to.deep.equal([ { name: 'User_userMethod1' }, { name: 'Post_postMethod1' }, ]); });