Coder Social home page Coder Social logo

cycle-apollo's People

Contributors

johnnynotsolucky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cycle-apollo's Issues

Made a bunch of changes (pull request)

Added some stuff and tests. I don't know if you care to keep this maintained so I though I'd ask before making pull request.

changelog
category alias to cat
callbacks to client
subscriptions add
select by operation name
observer and original object added into response, while first data entry got remapped under result
returns flattened results

tests:

import {
	makeApolloDriver,
	appendGraphOperationNameToOptions
} from "./cycleApollo.js";
import { setup, run } from "@cycle/run";
import runRxjs from "@cycle/rxjs-run";
import xs, { Stream } from "xstream";
import { setAdapt } from "@cycle/run/lib/adapt";

import { makeExecutableSchema, addMockFunctionsToSchema } from "graphql-tools";
import { graphql } from "graphql";
import schemaString from "./schemaTest.js";
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { SchemaLink } from "apollo-link-schema";
import gql from "graphql-tag";
import { adapt } from "@cycle/run/lib/adapt";

//remember test
import { Observable, of, never, interval, empty } from "rxjs";
import {
	map,
	mergeMap,
	switchMap,
	publishReplay,
	refCount,
	take,
	delay,
	mergeAll
} from "rxjs/operators";

let dispose = () => {};
var client: any = {};

const HERO = gql`
	query getHero {
		hero {
			name
		}
	}
`;

interface hero {
	data: {
		hero: {
			name: "Hello World";
		};
	};
}
function heroTest(x: hero, custom?: string) {
	expect(x).toMatchObject({
		data: {
			hero: {
				name: custom || "Hello World"
			}
		}
	});
}

const REVIEWEPISODE = gql`
	mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
		createReview(episode: $ep, review: $review) {
			episode
			stars
			commentary
		}
	}
`;

const reviewEpisodeVars = {
	ep: "JEDI",
	review: {
		stars: 5,
		commentary: "This is a great movie!"
	}
};

function reviewTest(x) {
	expect(x).toMatchObject({
		data: {
			createReview: {
				episode: "EMPIRE",
				commentary: "Hello World"
			}
		}
	});
}

describe("apolloDriver", function() {
	beforeAll(() => {
		// Make a GraphQL schema with no resolvers
		// import { mockServer, MockList } from 'graphql-tools';
		//lightweight variant requiring custom definitions to deterministic results
		const schema = makeExecutableSchema({ typeDefs: schemaString });
		// Add mocks, modifies schema in place
		const mocks = {
			Int: () => 6,
			Float: () => 22.1,
			String: () => "Hello World"
		};
		addMockFunctionsToSchema({ schema, mocks });
		const apolloCache = new InMemoryCache((window as any).__APOLLO_STATE__);
		// @ts-ignore
		client = new ApolloClient({
			// @ts-ignore
			cache: apolloCache,
			// @ts-ignore
			link: new SchemaLink({ schema })
		});
	});
	beforeEach(async function() {
		setAdapt((x) => x);
		await client.resetStore();
	});
	afterEach(function() {
		dispose();
	});
	it("will filter queries with select()", (done) => {
		function main(sources) {
			sources.apollo.select("category").subscribe({
				next(result) {
					heroTest(result);
					done();
				},
				error: done.fail,
				complete: () => done.fail("completed")
			});
			return {
				apollo: xs.of({
					query: HERO,
					category: "category"
				})
			};
		}

		dispose = run(main, { apollo: makeApolloDriver(client) });
	});
	it("will select by name given to operation in gql string", (done) => {
		function main(sources) {
			sources.apollo.select("getHero").subscribe({
				next(result) {
					heroTest(result);
					done();
				},
				error: done.fail,
				complete: () => done.fail("completed")
			});
			return {
				apollo: xs.of({
					query: HERO,
					category: "asdf"
				})
			};
		}

		dispose = run(main, { apollo: makeApolloDriver(client) });
	});
	it("will throw error if no category or operation name given within gql string in template or runtime, select can't occur", () => {
		expect(() =>
			appendGraphOperationNameToOptions({
				query: gql`
					query {
						hero {
							name
						}
					}
				`
			})
		).toThrow(
			"Neither category, nor operation name has been defined, so .select() won't trigger. Perhaps something went wrong"
		);
	});
	it.only("will accept async operations to be performed on client", (done) => {
		function main(sources) {
			var query$ = xs.of({
				query: HERO,
				category: "asdf"
			});

			var manipulate$ = xs.of({
				cat: "wQ",
				op: function(client) {
					var param = {
						query: HERO,
						data: { hero: { __typename: "Human", name: "asdff" } }
					};
					var a = client.writeQuery(param);
					var producer = {
						start: function(listener) {
							listener.next("yo");
						},

						stop: function() {
							clearInterval(this.id);
						},

						id: 0
					};
					return xs.create(producer);
				}
			});

			var then$ = sources.apollo
				.select("wQ")
				.mapTo(query$)
				.flatten();

			sources.apollo.select("asdf").subscribe({
				next(result) {
					heroTest(result, "asdff");
					done();
				},
				error: done.fail,
				complete: () => done.fail("completed")
			});

			return {
				apollo: xs.merge(manipulate$, then$)
			};
		}

		dispose = run(main, { apollo: makeApolloDriver(client) });
	}, 3000);
	it.skip("adapt works", function(done) {
		function main(_sources: any) {
			of(1);
			expect(_sources.apollo.select()).toHaveProperty(null);

			return {};
		}

		var select = () => {
			return adapt(xs.of("asdf"));
		};

		dispose = runRxjs(main, {
			apollo: () => {
				return { select };
			}
		});
	});
	it.skip("will rerun watchQuery after storeReset with empty results", () => {
		expect(true).toBe(true);
	});
	//coppied from http driver on cyclejs repository
	it.skip("should not remember past responses when selecting", function(done) {
		function main(_sources: any) {
			const test$ = of(null).pipe(
				delay(1000),
				mergeMap(() =>
					_sources.apollo.select("cat").pipe(
						mergeAll(),
						map((res: any) => "I should not show this, " + res.text)
					)
				)
			);

			const request$ = of({
				category: "cat",
				query: HERO
			});

			return {
				apollo: request$,
				Test: test$
			};
		}

		function testDriver(sink: any) {
			sink.addListener({
				next: (s: any) => {
					console.log(s);
					done.fail("No data should come through the Test sink");
				},
				error: (err: any) => {
					done(err);
				}
			});
		}

		dispose = runRxjs(main, {
			apollo: makeApolloDriver(client),
			Test: testDriver
		});

		setTimeout(() => {
			done();
		}, 2000);
	}, 4000); //from 277 on index.ts of http/test/browser
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.