How to use the apollo-link.Observable.of function in apollo-link

To help you get started, we’ve selected a few apollo-link examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github apollographql / apollo-link-state / packages / apollo-link-state / src / index.ts View on Github external
// TODO: the proper thing to do here is throw an error saying to
        // add `client.onResetStore(link.writeDefaults);`
        // waiting on https://github.com/apollographql/apollo-client/pull/3010

        return (
          // Support nested fields
          (aliasNeeded ? aliasedNode : preAliasingNode) ||
          (defaults || {})[fieldName]
        );
      };

      if (server) operation.query = server;
      const obs =
        server && forward
          ? forward(operation)
          : Observable.of({
              data: {},
            });

      return new Observable(observer => {
        // Works around race condition between completion and graphql execution
        // finishing. If complete is called during the graphql call, we will
        // miss out on the result, since the observer will have completed
        let complete = false;
        let handlingNext = false;
        obs.subscribe({
          next: ({ data, errors }) => {
            const observerErrorHandler = observer.error.bind(observer);
            const context = operation.getContext();

            handlingNext = true;
            //data is from the server and provides the root value to this GraphQL resolution
github strvcom / dep-manager-web / src / utils / RestLink.ts View on Github external
public request (operation: Operation, forward?: NextLink) {
    const isRestQuery = hasDirectives(['rest'], operation.query)
    if (!isRestQuery && forward) return forward(operation)
    const nonRestQuery = removeRestDirective(operation.query)
    const typeNamedQuery = addTypenameToDocument(operation.query)
    const observable =
      nonRestQuery && forward
        ? // tslint:disable-next-line:prefer-object-spread
        forward(Object.assign(operation, { query: nonRestQuery }))
        : Observable.of({ data: {} })
    return observable.flatMap(
      ({ data, ...rest }) =>
        new Observable(subscriber => {
          graphql(
            resolver,
            typeNamedQuery,
            data,
            this.resolverMap,
            operation.variables
          )
            .then(resolvedData => {
              subscriber.next({ data: resolvedData, ...rest })
              subscriber.complete()
            })
            .catch(err => {
              if (err.name === 'AbortError') return
github apollographql / apollo-client / packages / apollo-client / src / __tests__ / client.ts View on Github external
request => {
        expect(request.operationName).toBe('myMutationName');
        return Observable.of({ data });
      },
    ]);
github apollographql / apollo-client / packages / apollo-client / src / __tests__ / local-state / resolvers.ts View on Github external
const link = new ApolloLink(() =>
      Observable.of({ data: { baz: { foo: true, __typename: 'Baz' } } }),
    );
github apollographql / apollo-client / packages / apollo-client / src / core / __tests__ / QueryManager / links.ts View on Github external
const link = new ApolloLink((operation, forward) => {
      const { getCacheKey } = operation.getContext();
      expect(getCacheKey).toBeDefined();
      expect(getCacheKey({ id: 1, __typename: 'Book' })).toEqual('Book:1');
      return Observable.of({ data: bookData });
    });
github apollographql / apollo-link / packages / apollo-link-retry / src / __tests__ / retryLink.ts View on Github external
    const stub = jest.fn(() => Observable.of(data));
    const link = ApolloLink.from([retry, stub]);
github apollographql / apollo-link / packages / apollo-link-batch / src / __tests__ / batchLink.ts View on Github external
const terminating = new ApolloLink(operation => {
      try {
        expect(operation.query).toEqual(query);
      } catch (e) {
        done.fail(e);
      }
      return Observable.of(operation.variables.count);
    });
github apollographql / apollo-link / packages / apollo-link-batch / src / __tests__ / batchLink.ts View on Github external
      () => new BatchLink({ batchHandler: () => Observable.of() }),
    ).not.toThrow();
github apollographql / apollo-client-devtools / src / backend / links.js View on Github external
new ApolloLink((operation, forward) => {
    if (fetchPolicy === "no-cache") return forward(operation);

    const { cache } = operation.getContext();
    const { variables, query } = operation;
    try {
      const results = cache.readQuery({ query, variables });
      if (results) return Observable.of({ data: results });
    } catch (e) {}

    return forward(operation);
  });
github awslabs / aws-mobile-appsync-sdk-js / packages / aws-appsync / src / utils / index.ts View on Github external
export const passthroughLink = (op, forward) => (forward ? forward(op) : Observable.of());