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

Commit

Permalink
feat: allow strings as query input
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
Daniel Schmidt authored and DanielMSchmidt committed Jan 22, 2019
1 parent 5e5cd09 commit 792edb1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import "./styles.css";
import graphql from "reactive-graphql";

import { makeExecutableSchema } from "graphql-tools";
import gql from "graphql-tag";
import { from, interval, of } from "rxjs";
import { map, merge, scan, combineLatest } from "rxjs/operators";
import { componentFromStream } from "@dcos/data-service";
Expand Down Expand Up @@ -66,7 +65,7 @@ const schema = makeExecutableSchema({
}
});

const query = gql`
const query = `
query {
posts {
title
Expand Down Expand Up @@ -111,7 +110,7 @@ ReactDOM.render(<App />, rootElement);

## API

The first argument you pass into `reactive-graphql` is an executable schema, the second one a parsed GraphQL query. You can pass in the root context as an object as a third parameter. The variables can be passed as 4th parameter.
The first argument you pass into `reactive-graphql` is an executable schema, the second one a GraphQL query, either parsed or as string. You can pass in the root context as an object as a third parameter. The variables can be passed as 4th parameter.

The implementation will always return an Observable.
If any of the resolvers returns an error the implementation will emit the error on the stream.
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/graphqlObservable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ describe("graphqlObservable", function() {
m.expect(result.pipe(take(1))).toBeObservable(expected);
});

itMarbles("solves listing all fields with string query", function(m) {
const query = `
query {
launched {
name
}
}
`;

const expectedData = [{ name: "discovery" }];
const dataSource = of(expectedData);
const expected = m.cold("(a|)", {
a: { data: { launched: expectedData } }
});

const result = graphql(schema, query, { query: dataSource });

m.expect(result.pipe(take(1))).toBeObservable(expected);
});

itMarbles("filters by variable argument", function(m) {
const query = gql`
query {
Expand Down
21 changes: 17 additions & 4 deletions src/reactive-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
ArgumentNode,
isScalarType,
isEnumType,
isObjectType
isObjectType,
parse
} from "graphql";

// WARNING: This is NOT a spec complete graphql implementation
Expand Down Expand Up @@ -67,12 +68,24 @@ function isFieldWithResolver(

export default function graphql<T = object>(
schema: Schema,
doc: DocumentNode,
query: string | DocumentNode,
context: object = {},
variables: object = {}
): Observable<{ data: T }> {
): Observable<{ data?: T; errors?: string[] }> {
// Parse
let doc;
if (typeof query === "string") {
try {
doc = parse(query);
} catch (syntaxError) {
return of({ errors: [syntaxError] });
}
} else {
doc = query;
}

if (doc.definitions.length !== 1) {
return throwObservable("document root must have a single definition");
return throwObservable("query must have a single definition as root");
}

if (!schema._typeMap) {
Expand Down

0 comments on commit 792edb1

Please sign in to comment.