本文整理汇总了TypeScript中graphql.graphql函数的典型用法代码示例。如果您正苦于以下问题:TypeScript graphql函数的具体用法?TypeScript graphql怎么用?TypeScript graphql使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了graphql函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
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([]);
});
示例2: async
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']);
});
示例3: it
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
}
});
});
示例4: it
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'
}]);
});
示例5: Observable
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));
});
示例6: it
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();
});
});
示例7: it
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',
},
},
});
});
示例8: it
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();
});
示例9: exportPostGraphQLSchema
/**
* 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))
}
}
示例10: it
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' },
]);
});