diff --git a/src/__tests__/graphqlObservable-test.ts b/src/__tests__/graphqlObservable-test.ts index ac00bd6..605748b 100644 --- a/src/__tests__/graphqlObservable-test.ts +++ b/src/__tests__/graphqlObservable-test.ts @@ -219,7 +219,8 @@ describe("graphqlObservable", function() { const nameFilter = of("apollo11"); const result = graphqlObservable(query, schema, { - query: dataSource, + query: dataSource + }, { nameFilter }); @@ -630,11 +631,12 @@ describe("graphqlObservable", function() { `; const commandContext = of("a resquest"); - + const variables = { + name: "RocketShip" + }; const result = graphqlObservable(mutation, schema, { mutation: commandContext, - name: "RocketShip" - }); + }, variables); const expected = m.cold("(a|)", { a: { data: { shut: { name: "RocketShip" } } } diff --git a/src/__tests__/reference/starWarsQuery-test.ts b/src/__tests__/reference/starWarsQuery-test.ts index 2a82fe5..37fb0b3 100644 --- a/src/__tests__/reference/starWarsQuery-test.ts +++ b/src/__tests__/reference/starWarsQuery-test.ts @@ -4,12 +4,12 @@ import { take } from "rxjs/operators"; import StarWarsSchema from "./starWarsSchema"; import graphqlObservable from "../../"; -const graphql = (schema, query, _arg1?, _arg2?, params?) => { +const graphql = (schema, query, _rootValue?, contextValue?, variableValues?) => { return new Promise(resolve => { const taggedQuery = gql` ${query} `; - graphqlObservable(taggedQuery, schema, params) + graphqlObservable(taggedQuery, schema, contextValue, variableValues) .pipe(take(1)) .subscribe(resolve); }); diff --git a/src/index.ts b/src/index.ts index 146a9e1..3cf79f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,7 +68,8 @@ function isFieldWithResolver( export default function graphqlObservable( doc: DocumentNode, schema: Schema, - context: object + context: object = {}, + variables: object = {}, ): Observable<{ data: T }> { if (doc.definitions.length !== 1) { return throwObservable("document root must have a single definition"); @@ -80,7 +81,7 @@ export default function graphqlObservable( const types = schema._typeMap; - return resolve(doc.definitions[0], context, null, null).pipe( + return resolve(doc.definitions[0], context, variables, null, null).pipe( map((data: T) => ({ data })) @@ -89,13 +90,14 @@ export default function graphqlObservable( function resolve( definition: SchemaNode, context: object, + variables: object, parent: any, type: GraphQLType | null ) { if (isOperationDefinition(definition)) { const nextType = getResultType(type, definition, parent); - return resolveResult(definition, context, null, nextType); + return resolveResult(definition, context, variables, null, nextType); } // The definition gives us the field to resolve @@ -117,6 +119,7 @@ export default function graphqlObservable( field, definition, context, + variables, parent ); @@ -132,11 +135,11 @@ export default function graphqlObservable( } if (emitted instanceof Array) { - return resolveArrayResults(definition, context, emitted, type); + return resolveArrayResults(definition, context, variables, emitted, type); } const nextType = getResultType(type, definition, emitted); - return resolveResult(definition, context, emitted, nextType); + return resolveResult(definition, context, variables, emitted, nextType); }) ); } @@ -151,6 +154,7 @@ export default function graphqlObservable( function resolveResult( definition: SchemaNode, context: object, + variables: object, parent: any, type: GraphQLType | null ): Observable { @@ -174,7 +178,7 @@ export default function graphqlObservable( return throwObservable("Unsupported use of fragments in selection set"); } - const result = resolve(sel, context, parent, type); + const result = resolve(sel, context, variables, parent, type); const fieldName = (sel.alias || sel.name).value; return acc.pipe(combineLatest(result, objectAppendWithKey(fieldName))); @@ -184,6 +188,7 @@ export default function graphqlObservable( function resolveArrayResults( definition: SchemaNode, context: object, + variables: object, parents: any[], parentType: GraphQLType | null ) { @@ -192,6 +197,7 @@ export default function graphqlObservable( const resultObserver = resolveResult( definition, context, + variables, result, nextType ); @@ -269,13 +275,13 @@ function throwObservable(error: string): Observable { return throwError(new Error(`reactive-graphql: ${error}`)); } -function buildResolveArgs(definition: FieldNode, context: object) { +function buildResolveArgs(definition: FieldNode, variables: object) { return (definition.arguments || []).reduce( (carry, arg) => ({ ...carry, ...(arg.value.kind === Kind.VARIABLE ? // @ts-ignore - { [arg.name.value]: context[arg.value.name.value] } + { [arg.name.value]: variables[arg.value.name.value] } : { [arg.name.value]: getArgValue(arg) }) @@ -311,13 +317,14 @@ function resolveField( field: GraphQLField, definition: FieldNode, context: object, + variables: object, parent: any ): Observable { if (!isFieldWithResolver(field)) { return of(parent[field.name]); } - const args = buildResolveArgs(definition, context); + const args = buildResolveArgs(definition, variables); try { const resolvedValue = field.resolve( parent,