本文整理汇总了TypeScript中apollo-link.execute函数的典型用法代码示例。如果您正苦于以下问题:TypeScript execute函数的具体用法?TypeScript execute怎么用?TypeScript execute使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了execute函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('errors on an incorrect number of results for a batch', done => {
const link = new BatchHttpLink({
uri: 'batch',
batchInterval: 0,
batchMax: 3,
});
let errors = 0;
const next = data => {
done.fail('next should not have been called');
};
const complete = () => {
done.fail('complete should not have been called');
};
const error = error => {
errors++;
if (errors === 3) {
done();
}
};
execute(link, { query: sampleQuery }).subscribe(next, error, complete);
execute(link, { query: sampleQuery }).subscribe(next, error, complete);
execute(link, { query: sampleQuery }).subscribe(next, error, complete);
});
示例2: it
it(`does not affect different queries`, () => {
const document: DocumentNode = gql`
query test1($x: String) {
test(x: $x)
}
`;
const variables1 = { x: 'Hello World' };
const variables2 = { x: 'Goodbye World' };
const request1: GraphQLRequest = {
query: document,
variables: variables1,
operationName: getOperationName(document),
};
const request2: GraphQLRequest = {
query: document,
variables: variables2,
operationName: getOperationName(document),
};
let called = 0;
const deduper = ApolloLink.from([
new DedupLink(),
new ApolloLink(() => {
called += 1;
return null;
}),
]);
execute(deduper, request1);
execute(deduper, request2);
expect(called).toBe(2);
});
示例3: it
it('should call next with multiple results for subscription', done => {
const results = [
{ data: { data: 'result1' } },
{ data: { data: 'result2' } },
];
const client: any = {};
client.__proto__ = SubscriptionClient.prototype;
client.request = jest.fn(() => {
const copy = [...results];
return new Observable<ExecutionResult>(observer => {
observer.next(copy[0]);
observer.next(copy[1]);
});
});
const link = new WebSocketLink(client);
execute(link, { query: subscription }).subscribe(data => {
expect(client.request).toHaveBeenCalledTimes(1);
expect(data).toEqual(results.shift());
if (results.length === 0) {
done();
}
});
});
示例4: it
it("throws for GET if the extensions can't be stringified", done => {
const link = createHttpLink({
uri: 'http://data/',
useGETForQueries: true,
includeExtensions: true,
});
let b;
const a = { b };
b = { a };
a.b = b;
const extensions = {
a,
b,
};
execute(link, { query: sampleQuery, extensions }).subscribe(
result => {
done.fail('next should have been thrown from the link');
},
makeCallback(done, e => {
expect(e.message).toMatch(/Extensions map is not serializable/);
expect(e.parseError.message).toMatch(
/Converting circular structure to JSON/,
);
}),
);
});
示例5: it
it('captures errors within links', done => {
const query = gql`
query Foo {
foo {
bar
}
}
`;
let called;
const errorLink = onError(({ operation, networkError }) => {
expect(networkError.message).toBe('app is crashing');
expect(operation.operationName).toBe('Foo');
called = true;
});
const mockLink = new ApolloLink(operation => {
return new Observable(obs => {
throw new Error('app is crashing');
});
});
const link = errorLink.concat(mockLink);
execute(link, { query }).subscribe({
error: e => {
expect(e.message).toBe('app is crashing');
expect(called).toBe(true);
done();
},
});
});
示例6: it
it('passes a query on to the next link', done => {
const nextLink = new ApolloLink(operation => {
try {
expect(operation.getContext()).toMatchSnapshot();
expect(print(operation.query)).toEqual(
print(gql`
query Mixed {
bar {
foo
}
}
`),
);
} catch (error) {
done.fail(error);
}
return Observable.of({ data: { bar: { foo: true } } });
});
const client = withClientState({ resolvers });
execute(client.concat(nextLink), { query: mixedQuery }).subscribe(
() => done(),
done.fail,
);
});
示例7: execute
[1, 2, 3, 4].forEach(x => {
execute(link, {
query,
}).subscribe({
next: d => {
try {
expect(d).toEqual(data);
} catch (e) {
done.fail(e);
}
},
error: done.fail,
complete: () => {
count++;
if (count === 4) {
try {
expect(batchHandler.mock.calls.length).toBe(2);
done();
} catch (e) {
done.fail(e);
}
}
},
});
});
示例8: it
it('calls unsubscribe on the appropriate downstream observable', async () => {
const retry = new RetryLink({
delay: { initial: 1 },
attempts: { max: 2 },
});
const data = { data: { hello: 'world' } };
const unsubscribeStub = jest.fn();
const firstTry = fromError(standardError);
// Hold the test hostage until we're hit
let secondTry;
const untilSecondTry = new Promise(resolve => {
secondTry = {
subscribe(observer) {
resolve(); // Release hold on test.
Promise.resolve().then(() => {
observer.next(data);
observer.complete();
});
return { unsubscribe: unsubscribeStub };
},
};
});
const stub = jest.fn();
stub.mockReturnValueOnce(firstTry);
stub.mockReturnValueOnce(secondTry);
const link = ApolloLink.from([retry, stub]);
const subscription = execute(link, { query }).subscribe({});
await untilSecondTry;
subscription.unsubscribe();
expect(unsubscribeStub).toHaveBeenCalledTimes(1);
});
示例9: it
it('passes forward on', done => {
const link = ApolloLink.from([
new BatchLink({
batchInterval: 0,
batchMax: 1,
batchHandler: (operation, forward) => {
try {
expect(forward.length).toBe(1);
expect(operation.length).toBe(1);
} catch (e) {
done.fail(e);
}
return forward[0](operation[0]).map(result => [result]);
},
}),
new ApolloLink(operation => {
terminatingCheck(done, () => {
expect(operation.query).toEqual(query);
})();
return null;
}),
]);
execute(
link,
createOperation(
{},
{
query,
},
),
).subscribe(result => done.fail());
});
示例10: it
it('should poll request', done => {
let count = 0;
let subscription;
const spy = jest.fn();
const checkResults = () => {
const calls = spy.mock.calls;
calls.map((call, i) => expect(call[0].data.count).toEqual(i));
expect(calls.length).toEqual(5);
done();
};
const poll = new PollingLink(() => 1).concat(() => {
if (count >= 5) {
subscription.unsubscribe();
checkResults();
}
return Observable.of({
data: {
count: count++,
},
});
});
subscription = execute(poll, { query }).subscribe({
next: spy,
error: error => {
throw error;
},
complete: () => {
throw new Error();
},
});
});
示例11: it
it('concatenates schema strings if typeDefs are passed in as an array', done => {
const anotherSchema = `
type Foo {
foo: String!
bar: String
}
`;
const nextLink = new ApolloLink(operation => {
const { schemas } = operation.getContext();
expect(schemas).toMatchSnapshot();
return Observable.of({
data: { foo: { bar: true, __typename: 'Bar' } },
});
});
const client = withClientState({
resolvers,
defaults,
typeDefs: [typeDefs, anotherSchema],
});
execute(client.concat(nextLink), {
query: remoteQuery,
}).subscribe(() => done(), done.fail);
});
示例12: it
it('respects aliases for *nested fields* on the @client-tagged node', done => {
const aliasedQuery = gql`
query Test {
fie: foo @client {
fum: bar
}
baz: bar {
foo
}
}
`;
const clientLink = withClientState({
resolvers: {
Query: {
foo: () => ({ bar: true }),
fie: () => {
done.fail(
"Called the resolver using the alias' name, instead of the correct resolver name.",
);
},
},
},
});
const sample = new ApolloLink(() =>
Observable.of({ data: { baz: { foo: true } } }),
);
execute(clientLink.concat(sample), {
query: aliasedQuery,
}).subscribe(({ data }) => {
expect(data).toEqual({ fie: { fum: true }, baz: { foo: true } });
done();
}, done.fail);
});
示例13: execute
complete: () => {
execute(deduper, request2).subscribe({
complete: () => {
expect(called).toBe(2);
done();
},
});
},
示例14: execute
[1, 2].forEach(x => {
execute(link, {
query,
variables: { endpoint: 'rofl' },
}).subscribe({
next: next(roflData),
error: done.fail,
complete,
});
execute(link, {
query,
variables: { endpoint: 'lawl' },
}).subscribe({
next: next(lawlData),
error: done.fail,
complete,
});
});
示例15: execute
execute(client, { query }).subscribe(({ data }) => {
expect(data).toEqual({ foo: { bar: 1 } });
// twice
execute(client, { query }).subscribe(({ data }) => {
expect(data).toEqual({ foo: { bar: 1 } });
expect(resolversSpy).toHaveBeenCalledTimes(2);
done();
}, done.fail);
}, done.fail);