UNPKG

@sterblue/sterblue-sdk

Version:

Sterblue Graph SDK for graphile.sterblue.com

216 lines (204 loc) 6.62 kB
"use strict"; var _fp = require("lodash/fp"); var _streamingIterables = require("streaming-iterables"); var _sdk = require("../sdk"); var _client = require("@apollo/client"); async function setupSdk() { return await (0, _sdk.getApolloClientSdk)({ email: "vincent.lecrubier@sterblue.com", password: "underline-tiny-brown-carry-ride", uri: "https://beta.graphile.sterblue.com/graphql" }); } describe("getApolloClientSdk", () => { test("list missions should return a list of desired size", async () => { const sdk = await setupSdk(); const { list } = await sdk.listMission({ first: 2 }); expect((0, _fp.size)(list)).toEqual(2); }); test("count missions should return the number of missions", async () => { const sdk = await setupSdk(); const { connection: { totalCount } } = await sdk.countMission({ first: 2 }); // Returns the total number, 26 in this case expect(totalCount).toBeGreaterThan(2); }); test("stream missions should return a list of desired size", async () => { const sdk = await setupSdk(); const result = await (0, _streamingIterables.collect)(await sdk.Mission.stream({ first: 2 })); expect((0, _fp.size)(result)).toEqual(2); }); test.each(["query", "mutate", "subscribe"])("should expose %p function from client", async property => { const sdk = await setupSdk(); expect(sdk).toHaveProperty(property); expect(sdk[property]).toBeDefined(); expect((0, _fp.isFunction)(sdk[property])).toBeTruthy(); }); test("should expose usable mutate function", async () => { const sdk = await setupSdk(); const result = await sdk.mutate({ mutation: (0, _client.gql)` mutation authenticateUser($email: String!, $password: String!) { authenticateUser(input: { email: $email, password: $password }) { jwtUserToken } } `, variables: { email: "vincent.lecrubier@sterblue.com", password: "underline-tiny-brown-carry-ride" } }); expect(result.data.authenticateUser).toHaveProperty("jwtUserToken"); }); test("should expose stream function", async () => { const sdk = await setupSdk(); const result = await (0, _streamingIterables.collect)(await sdk.stream({ query: (0, _client.gql)` query listMission( $first: Int! = 20 $offset: Int! = 0 $filter: MissionFilter $orderBy: [MissionsOrderBy!] ) { missions( first: $first offset: $offset filter: $filter orderBy: $orderBy ) { id name createdAt plan { id } execution { id } } } `, variables: { first: 2, filter: {} // orderBy: [] } })); expect((0, _fp.size)(result)).toEqual(2); }); test("should expose clear errors", async () => { const sdk = await setupSdk(); // It does throw clear errors, see below, but it prints them in the console! await expect(sdk.query({ query: (0, _client.gql)` query getImage { images(first: 10) { id toot } } `, variables: {} })).rejects.toThrow("Response not successful: Received status code 400"); await expect((0, _streamingIterables.collect)(await sdk.stream({ query: (0, _client.gql)` query getImage($first: Int! = 20, $offset: Int! = 0) { images(first: $first, offset: $offset) { id toot } } `, variables: {} }))).rejects.toThrow("Response not successful: Received status code 400"); }); test("case causing problem in sdk use", async () => { const sdk = await setupSdk(); const resultIterator = await sdk.stream({ // This is avery stupid query, just for example query: (0, _client.gql)` # The query MUST declare the first and offset variables query getImagesInPolygon( $first: Int $offset: Int # $polygon: GeoJSON # $filter: ImageFilter! # $orderBy: [ImagesOrderBy!] ) { # There MUST be somewhere in the query the use of offset and first images( first: $first offset: $offset # filter: { # and: [$filter, { location: { geometry: { intersects: $polygon } } }] # } # orderBy: $orderBy ) { id location { geometry { geojson } } } } `, variables: { first: 10 // You can "infinite stream" through ALL the results in the database if you remove "first" // offset: 0 // filter: { id: { notEqualTo: null } }, // polygon: { // type: "Polygon", // coordinates: [ // [ // [-26.894531249999996, 57.42129439209407], // [-41.8359375, 18.646245142670608], // [-2.8125, -2.108898659243126], // [21.62109375, 38.95940879245423], // [-4.5703125, 60.06484046010452], // [-26.894531249999996, 57.42129439209407] // ] // ] // } } }); // Collect the output of the processing in one array const result = await (0, _streamingIterables.collect)(resultIterator); expect((0, _fp.size)(result)).toEqual(10); }); // FIXME this is a known limitation for now aliad of graphql fields are not well supported... test.skip("should expose stream function working with alias", async () => { const sdk = await setupSdk(); const result = await (0, _streamingIterables.collect)(await sdk.stream({ query: (0, _client.gql)` query listMission( $first: Int! = 20 $offset: Int! = 0 $filter: MissionWhereInput $orderBy: MissionOrderByInput ) { list: missions( first: $first offset: $offset filter: $filter orderBy: $orderBy ) { id name createdAt plan { id } execution { id } } } `, variables: { first: 2 } })); expect((0, _fp.size)(result)).toEqual(2); }); });