本文整理汇总了TypeScript中apollo-fetch.createApolloFetch函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createApolloFetch函数的具体用法?TypeScript createApolloFetch怎么用?TypeScript createApolloFetch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createApolloFetch函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('returns thrown context error as a valid graphql result', async () => {
const nodeEnv = process.env.NODE_ENV;
delete process.env.NODE_ENV;
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => {
throw Error('never get here');
},
},
};
server = new ApolloServer({
typeDefs,
resolvers,
context: () => {
throw new AuthenticationError('valid result');
},
});
app = new Server({ port });
await server.applyMiddleware({
app,
disableHealthCheck: true,
});
await app.start();
httpServer = app.listener;
const uri = app.info.uri + '/graphql';
const apolloFetch = createApolloFetch({ uri });
const result = await apolloFetch({ query: '{hello}' });
expect(result.errors.length).toEqual(1);
expect(result.data).toBeUndefined();
const e = result.errors[0];
expect(e.message).toMatch('valid result');
expect(e.extensions).toBeDefined();
expect(e.extensions.code).toEqual('UNAUTHENTICATED');
expect(e.extensions.exception.stacktrace).toBeDefined();
process.env.NODE_ENV = nodeEnv;
});
示例2: Promise
return new Promise((resolve, reject) => {
const fetch = createApolloFetch({
uri: url
});
fetch({
query: '{ hello }',
}).then(res => {
if ( res.data === undefined ) {
reject('Failed to call hello');
}
resolve(res.data.hello);
}).catch( err => {
reject(err);
});
});
示例3: it
it('applies tracing extension with cache control enabled', async () => {
const { url: uri } = await createServer({
typeDefs,
resolvers,
tracing: true,
cacheControl: true,
});
const apolloFetch = createApolloFetch({ uri });
const result = await apolloFetch({
query: `{ books { title author } }`,
});
expect(result.data).toEqual({ books });
expect(result.extensions).toBeDefined();
expect(result.extensions.tracing).toBeDefined();
});
示例4: it
it('configures cors', async () => {
server = new ApolloServer({
typeDefs,
resolvers,
});
const { url: uri } = await server.listen();
const apolloFetch = createApolloFetch({ uri }).useAfter(
(response, next) => {
expect(
response.response.headers.get('access-control-allow-origin'),
).toEqual('*');
next();
},
);
await apolloFetch({ query: '{hello}' });
});