Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
feat(signature) accept variables parameter
Browse files Browse the repository at this point in the history
BREAKING CHANGE: context param was used to get resolver's arguments. It's not the case anymore.
  • Loading branch information
alexstrat authored and DanielMSchmidt committed Jan 21, 2019
1 parent 0164358 commit 168b0e8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/__tests__/graphqlObservable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ describe("graphqlObservable", function() {

const nameFilter = of("apollo11");
const result = graphqlObservable(query, schema, {
query: dataSource,
query: dataSource
}, {
nameFilter
});

Expand Down Expand Up @@ -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" } } }
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/reference/starWarsQuery-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
25 changes: 16 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function isFieldWithResolver(
export default function graphqlObservable<T = object>(
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");
Expand All @@ -80,7 +81,7 @@ export default function graphqlObservable<T = object>(

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
}))
Expand All @@ -89,13 +90,14 @@ export default function graphqlObservable<T = object>(
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
Expand All @@ -117,6 +119,7 @@ export default function graphqlObservable<T = object>(
field,
definition,
context,
variables,
parent
);

Expand All @@ -132,11 +135,11 @@ export default function graphqlObservable<T = object>(
}

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);
})
);
}
Expand All @@ -151,6 +154,7 @@ export default function graphqlObservable<T = object>(
function resolveResult(
definition: SchemaNode,
context: object,
variables: object,
parent: any,
type: GraphQLType | null
): Observable<any> {
Expand All @@ -174,7 +178,7 @@ export default function graphqlObservable<T = object>(
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)));
Expand All @@ -184,6 +188,7 @@ export default function graphqlObservable<T = object>(
function resolveArrayResults(
definition: SchemaNode,
context: object,
variables: object,
parents: any[],
parentType: GraphQLType | null
) {
Expand All @@ -192,6 +197,7 @@ export default function graphqlObservable<T = object>(
const resultObserver = resolveResult(
definition,
context,
variables,
result,
nextType
);
Expand Down Expand Up @@ -269,13 +275,13 @@ function throwObservable(error: string): Observable<any> {
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)
})
Expand Down Expand Up @@ -311,13 +317,14 @@ function resolveField(
field: GraphQLField<any, any, { [argName: string]: any }>,
definition: FieldNode,
context: object,
variables: object,
parent: any
): Observable<any> {
if (!isFieldWithResolver(field)) {
return of(parent[field.name]);
}

const args = buildResolveArgs(definition, context);
const args = buildResolveArgs(definition, variables);
try {
const resolvedValue = field.resolve(
parent,
Expand Down

0 comments on commit 168b0e8

Please sign in to comment.