|
| 1 | +import { execute, ApolloLink, Observable, from } from "apollo-link"; |
| 2 | +import { getQueryDefinition } from "apollo-utilities"; |
| 3 | +import gql from "graphql-tag"; |
| 4 | + |
| 5 | +/* |
| 6 | + * |
| 7 | + * supports dynamic client schemas set on the context |
| 8 | + * |
| 9 | + * schemas must be an array of the following shape |
| 10 | + * |
| 11 | + { |
| 12 | + definition: schemaString, |
| 13 | + directives: `directive @client on FIELD`, |
| 14 | + } |
| 15 | + * |
| 16 | + */ |
| 17 | +const apolloClientSchema = { |
| 18 | + directives: "directive @connection(key: String!, filter: [String]) on FIELD", |
| 19 | +}; |
| 20 | +const schemaLink = () => |
| 21 | + new ApolloLink((operation, forward) => { |
| 22 | + return forward(operation).map(result => { |
| 23 | + let { schemas = [] } = operation.getContext(); |
| 24 | + result.extensions = Object.assign({}, result.extensions, { |
| 25 | + schemas: schemas.concat([apolloClientSchema]), |
| 26 | + }); |
| 27 | + return result; |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | +// forward all "errors" to next with a good shape for graphiql |
| 32 | +const errorLink = () => |
| 33 | + new ApolloLink((operation, forward) => { |
| 34 | + return new Observable(observer => { |
| 35 | + let sub; |
| 36 | + try { |
| 37 | + sub = forward(operation).subscribe({ |
| 38 | + next: observer.next.bind(observer), |
| 39 | + error: networkError => |
| 40 | + observer.next({ |
| 41 | + errors: [ |
| 42 | + { |
| 43 | + message: networkError.message, |
| 44 | + locations: [networkError.stack], |
| 45 | + }, |
| 46 | + ], |
| 47 | + }), |
| 48 | + complete: observer.complete.bind(observer), |
| 49 | + }); |
| 50 | + } catch (e) { |
| 51 | + observer.next({ |
| 52 | + errors: [{ message: e.message, locations: [e.stack] }], |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + return () => { |
| 57 | + if (sub) sub.unsubscribe(); |
| 58 | + }; |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | +const cacheLink = fetchPolicy => |
| 63 | + new ApolloLink((operation, forward) => { |
| 64 | + // XXX how do we handle local state here? It *should* still work right? |
| 65 | + if (fetchPolicy === "no-cache") return forward(operation); |
| 66 | + |
| 67 | + const { cache } = operation.getContext(); |
| 68 | + const { variables, query } = operation; |
| 69 | + // quick check if this is a query |
| 70 | + try { |
| 71 | + const def = getQueryDefinition(query); |
| 72 | + const results = cache.readQuery({ query, variables }); |
| 73 | + if (results) return Observable.of({ data: results }); |
| 74 | + } catch (e) {} |
| 75 | + |
| 76 | + return forward(operation); |
| 77 | + }); |
| 78 | + |
| 79 | +export const initLinkEvents = (hook, bridge) => { |
| 80 | + // handle incoming requests |
| 81 | + const subscriber = request => { |
| 82 | + const { query, variables, operationName, key, fetchPolicy } = JSON.parse( |
| 83 | + request, |
| 84 | + ); |
| 85 | + try { |
| 86 | + const userLink = hook.ApolloClient.link; |
| 87 | + const cache = hook.ApolloClient.cache; |
| 88 | + |
| 89 | + const devtoolsLink = from([ |
| 90 | + errorLink(), |
| 91 | + cacheLink(fetchPolicy), |
| 92 | + schemaLink(), |
| 93 | + userLink, |
| 94 | + ]); |
| 95 | + const obs = execute(devtoolsLink, { |
| 96 | + query: gql(query), |
| 97 | + variables, |
| 98 | + operationName, |
| 99 | + context: { __devtools_key__: key, cache }, |
| 100 | + }); |
| 101 | + obs.subscribe({ |
| 102 | + next: data => bridge.send(`link:next:${key}`, JSON.stringify(data)), |
| 103 | + error: err => bridge.send(`link:error:${key}`, JSON.stringify(err)), |
| 104 | + complete: () => bridge.send(`link:complete:${key}`), |
| 105 | + }); |
| 106 | + } catch (e) { |
| 107 | + bridge.send(`link:error:${key}`, JSON.stringify(e)); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + bridge.on("link:operation", subscriber); |
| 112 | +}; |
0 commit comments