本文整理匯總了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}' });
});