Created
December 6, 2018 23:55
-
-
Save alloy/59b502ebdb77b907c8fe8bc2a2c4f12b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 519e45328200a0a23b37245bbe198f9d834407bf | |
Author: Eloy Durán <eloy.de.enige@gmail.com> | |
Date: Fri Dec 7 00:52:07 2018 +0100 | |
stash more | |
diff --git a/src/DevTools/createMockNetworkLayer/__tests__/createMockNetworkLayer.test.ts b/src/DevTools/createMockNetworkLayer/__tests__/createMockNetworkLayer.test.ts | |
index c82aeb5c..7f548964 100644 | |
--- a/src/DevTools/createMockNetworkLayer/__tests__/createMockNetworkLayer.test.ts | |
+++ b/src/DevTools/createMockNetworkLayer/__tests__/createMockNetworkLayer.test.ts | |
@@ -1,57 +1,53 @@ | |
+import { IResolvers } from "graphql-tools/dist/Interfaces" | |
import { graphql } from "react-relay" | |
-import { Environment, fetchQuery, RecordSource, Store } from "relay-runtime" | |
+import { | |
+ Environment, | |
+ fetchQuery, | |
+ GraphQLTaggedNode, | |
+ RecordSource, | |
+ Store, | |
+} from "relay-runtime" | |
import { createMockNetworkLayer } from "../index" | |
describe("createMockNetworkLayer", () => { | |
- describe("preserves the upstream behaviour", () => { | |
- it("returns the data if present", async () => { | |
- const network = createMockNetworkLayer({ | |
- Query: { artwork: () => ({ title: "lol", __id: "blah" }) }, | |
- }) | |
- const source = new RecordSource() | |
- const store = new Store(source) | |
- const environment = new Environment({ network, store }) | |
+ function fetchQueryWithResolvers( | |
+ resolvers: IResolvers, | |
+ query?: GraphQLTaggedNode | |
+ ) { | |
+ const network = createMockNetworkLayer(resolvers) | |
- const query = graphql` | |
- query createMockNetworkLayerQuery($artworkID: String!) { | |
- artwork(id: $artworkID) { | |
- title | |
- } | |
- } | |
- ` | |
+ const source = new RecordSource() | |
+ const store = new Store(source) | |
+ const environment = new Environment({ network, store }) | |
- const variables = { | |
- artworkID: "110798995619330", | |
- } | |
+ return fetchQuery( | |
+ environment, | |
+ query || | |
+ graphql` | |
+ query createMockNetworkLayerTestQuery { | |
+ artwork(id: "untitled") { | |
+ __id | |
+ title | |
+ } | |
+ } | |
+ `, | |
+ {} | |
+ ) | |
+ } | |
- await fetchQuery(environment, query, variables).then(data => { | |
- expect(data.artwork.title).toEqual("lol") | |
+ describe("preserves the upstream behaviour", () => { | |
+ it("returns the data if present", async () => { | |
+ const data = await fetchQueryWithResolvers({ | |
+ Query: { artwork: () => ({ title: "Untitled", __id: "untitled" }) }, | |
}) | |
+ expect(data.artwork.title).toEqual("Untitled") | |
}) | |
it("returns null for nullable fields which are given as null", async () => { | |
- const network = createMockNetworkLayer({ | |
- Query: { artwork: () => ({ title: null, __id: "id" }) }, | |
- }) | |
- const source = new RecordSource() | |
- const store = new Store(source) | |
- const environment = new Environment({ network, store }) | |
- | |
- const query = graphql` | |
- query createMockNetworkLayerQuery($artworkID: String!) { | |
- artwork(id: $artworkID) { | |
- title | |
- } | |
- } | |
- ` | |
- | |
- const variables = { | |
- artworkID: "110798995619330", | |
- } | |
- | |
- await fetchQuery(environment, query, variables).then(data => { | |
- expect(data.artwork.title).toEqual(null) | |
+ const data = await fetchQueryWithResolvers({ | |
+ Query: { artwork: () => ({ title: null, __id: "null" }) }, | |
}) | |
+ expect(data.artwork.title).toEqual(null) | |
}) | |
// // TODO: figure out what graphql-js does with `undefined` | |
@@ -89,27 +85,10 @@ describe("createMockNetworkLayer", () => { | |
}) | |
it("complains with a helpful error when selected field is not present", async () => { | |
- const network = createMockNetworkLayer({ | |
- Query: { artwork: () => ({ __id: "blah" }) }, | |
- }) | |
- const source = new RecordSource() | |
- const store = new Store(source) | |
- const environment = new Environment({ network, store }) | |
- | |
- const query = graphql` | |
- query createMockNetworkLayerQuery($artworkID: String!) { | |
- artwork(id: $artworkID) { | |
- title | |
- } | |
- } | |
- ` | |
- | |
- const variables = { | |
- artworkID: "110798995619330", | |
- } | |
- | |
try { | |
- await fetchQuery(environment, query, variables) | |
+ await fetchQueryWithResolvers({ | |
+ Query: { artwork: () => ({ __id: "blah" }) }, | |
+ }) | |
} catch (e) { | |
expect(e.message).toMatchInlineSnapshot( | |
`"RelayMockNetworkLayerError: A mock for field at path 'artwork/title' of type 'String' was expected but not found."` | |
@@ -118,35 +97,32 @@ describe("createMockNetworkLayer", () => { | |
}) | |
it("uses data provided with an aliased name", async () => { | |
- const network = createMockNetworkLayer({ | |
- Query: { | |
- artwork: () => ({ | |
- aliasedTitle1: "Untitled 1", | |
- aliasedTitle2: "Untitled 2", | |
- __id: "id", | |
- }), | |
+ const data = await fetchQueryWithResolvers( | |
+ { | |
+ Query: { | |
+ artist: () => ({ | |
+ forSaleArtworks: [{ __id: "for-sale-work" }], | |
+ notForSaleArtworks: [{ __id: "no-for-sale-work" }], | |
+ __id: "id", | |
+ }), | |
+ }, | |
}, | |
- }) | |
- const source = new RecordSource() | |
- const store = new Store(source) | |
- const environment = new Environment({ network, store }) | |
- | |
- const query = graphql` | |
- query createMockNetworkLayerQuery($artworkID: String!) { | |
- artwork(id: $artworkID) { | |
- aliasedTitle1: title | |
- aliasedTitle2: title | |
+ graphql` | |
+ query createMockNetworkLayerTestAliasQuery { | |
+ artist(id: "banksy") { | |
+ forSaleArtworks: artworks(filter: IS_FOR_SALE) { | |
+ __id | |
+ } | |
+ notForSaleArtworks: artworks(filter: IS_NOT_FOR_SALE) { | |
+ __id | |
+ } | |
+ } | |
} | |
- } | |
- ` | |
- | |
- const variables = { | |
- artworkID: "110798995619330", | |
- } | |
- | |
- await fetchQuery(environment, query, variables).then(data => { | |
- expect(data.artwork.aliasedTitle1).toEqual("Untitled 1") | |
- expect(data.artwork.aliasedTitle2).toEqual("Untitled 2") | |
- }) | |
+ ` | |
+ ) | |
+ expect(data.artist.forSaleArtworks).toEqual([{ __id: "for-sale-work" }]) | |
+ expect(data.artist.notForSaleArtworks).toEqual([ | |
+ { __id: "no-for-sale-work" }, | |
+ ]) | |
}) | |
}) | |
diff --git a/src/DevTools/createMockNetworkLayer/index.ts b/src/DevTools/createMockNetworkLayer/index.ts | |
index 26666409..53b148c4 100644 | |
--- a/src/DevTools/createMockNetworkLayer/index.ts | |
+++ b/src/DevTools/createMockNetworkLayer/index.ts | |
@@ -20,6 +20,10 @@ export const createMockNetworkLayer = (mockResolvers: IResolvers) => { | |
if (info.fieldName in source) { | |
return source[info.fieldName] | |
} | |
+ const alias = info.fieldNodes[0].alias | |
+ if (alias && alias.value in source) { | |
+ return source[alias.value] | |
+ } | |
} | |
complain(info, info.returnType.inspect()) | |
}) as GraphQLFieldResolver<any, any>, | |
diff --git a/src/__generated__/createMockNetworkLayerTestAliasQuery.graphql.ts b/src/__generated__/createMockNetworkLayerTestAliasQuery.graphql.ts | |
new file mode 100644 | |
index 00000000..e57bf7c8 | |
--- /dev/null | |
+++ b/src/__generated__/createMockNetworkLayerTestAliasQuery.graphql.ts | |
@@ -0,0 +1,126 @@ | |
+/* tslint:disable */ | |
+ | |
+import { ConcreteRequest } from "relay-runtime"; | |
+export type createMockNetworkLayerTestAliasQueryVariables = {}; | |
+export type createMockNetworkLayerTestAliasQueryResponse = { | |
+ readonly artist: ({ | |
+ readonly forSaleArtworks: ReadonlyArray<({ | |
+ readonly __id: string; | |
+ }) | null> | null; | |
+ readonly notForSaleArtworks: ReadonlyArray<({ | |
+ readonly __id: string; | |
+ }) | null> | null; | |
+ }) | null; | |
+}; | |
+export type createMockNetworkLayerTestAliasQuery = { | |
+ readonly response: createMockNetworkLayerTestAliasQueryResponse; | |
+ readonly variables: createMockNetworkLayerTestAliasQueryVariables; | |
+}; | |
+ | |
+ | |
+ | |
+/* | |
+query createMockNetworkLayerTestAliasQuery { | |
+ artist(id: "banksy") { | |
+ forSaleArtworks: artworks(filter: IS_FOR_SALE) { | |
+ __id | |
+ } | |
+ notForSaleArtworks: artworks(filter: IS_NOT_FOR_SALE) { | |
+ __id | |
+ } | |
+ __id | |
+ } | |
+} | |
+*/ | |
+ | |
+const node: ConcreteRequest = (function(){ | |
+var v0 = { | |
+ "kind": "ScalarField", | |
+ "alias": null, | |
+ "name": "__id", | |
+ "args": null, | |
+ "storageKey": null | |
+}, | |
+v1 = [ | |
+ v0 | |
+], | |
+v2 = [ | |
+ { | |
+ "kind": "LinkedField", | |
+ "alias": null, | |
+ "name": "artist", | |
+ "storageKey": "artist(id:\"banksy\")", | |
+ "args": [ | |
+ { | |
+ "kind": "Literal", | |
+ "name": "id", | |
+ "value": "banksy", | |
+ "type": "String!" | |
+ } | |
+ ], | |
+ "concreteType": "Artist", | |
+ "plural": false, | |
+ "selections": [ | |
+ { | |
+ "kind": "LinkedField", | |
+ "alias": "forSaleArtworks", | |
+ "name": "artworks", | |
+ "storageKey": "artworks(filter:\"IS_FOR_SALE\")", | |
+ "args": [ | |
+ { | |
+ "kind": "Literal", | |
+ "name": "filter", | |
+ "value": "IS_FOR_SALE", | |
+ "type": "[ArtistArtworksFilters]" | |
+ } | |
+ ], | |
+ "concreteType": "Artwork", | |
+ "plural": true, | |
+ "selections": v1 | |
+ }, | |
+ { | |
+ "kind": "LinkedField", | |
+ "alias": "notForSaleArtworks", | |
+ "name": "artworks", | |
+ "storageKey": "artworks(filter:\"IS_NOT_FOR_SALE\")", | |
+ "args": [ | |
+ { | |
+ "kind": "Literal", | |
+ "name": "filter", | |
+ "value": "IS_NOT_FOR_SALE", | |
+ "type": "[ArtistArtworksFilters]" | |
+ } | |
+ ], | |
+ "concreteType": "Artwork", | |
+ "plural": true, | |
+ "selections": v1 | |
+ }, | |
+ v0 | |
+ ] | |
+ } | |
+]; | |
+return { | |
+ "kind": "Request", | |
+ "operationKind": "query", | |
+ "name": "createMockNetworkLayerTestAliasQuery", | |
+ "id": null, | |
+ "text": "query createMockNetworkLayerTestAliasQuery {\n artist(id: \"banksy\") {\n forSaleArtworks: artworks(filter: IS_FOR_SALE) {\n __id\n }\n notForSaleArtworks: artworks(filter: IS_NOT_FOR_SALE) {\n __id\n }\n __id\n }\n}\n", | |
+ "metadata": {}, | |
+ "fragment": { | |
+ "kind": "Fragment", | |
+ "name": "createMockNetworkLayerTestAliasQuery", | |
+ "type": "Query", | |
+ "metadata": null, | |
+ "argumentDefinitions": [], | |
+ "selections": v2 | |
+ }, | |
+ "operation": { | |
+ "kind": "Operation", | |
+ "name": "createMockNetworkLayerTestAliasQuery", | |
+ "argumentDefinitions": [], | |
+ "selections": v2 | |
+ } | |
+}; | |
+})(); | |
+(node as any).hash = '7e9f3aa22daa4f72c6a1825d9f181356'; | |
+export default node; | |
diff --git a/src/__generated__/createMockNetworkLayerQuery.graphql.ts b/src/__generated__/createMockNetworkLayerTestQuery.graphql.ts | |
similarity index 50% | |
rename from src/__generated__/createMockNetworkLayerQuery.graphql.ts | |
rename to src/__generated__/createMockNetworkLayerTestQuery.graphql.ts | |
index 978076aa..87c8b4c2 100644 | |
--- a/src/__generated__/createMockNetworkLayerQuery.graphql.ts | |
+++ b/src/__generated__/createMockNetworkLayerTestQuery.graphql.ts | |
@@ -1,52 +1,41 @@ | |
/* tslint:disable */ | |
import { ConcreteRequest } from "relay-runtime"; | |
-export type createMockNetworkLayerQueryVariables = { | |
- readonly artworkID: string; | |
-}; | |
-export type createMockNetworkLayerQueryResponse = { | |
+export type createMockNetworkLayerTestQueryVariables = {}; | |
+export type createMockNetworkLayerTestQueryResponse = { | |
readonly artwork: ({ | |
+ readonly __id: string; | |
readonly title: string | null; | |
}) | null; | |
}; | |
-export type createMockNetworkLayerQuery = { | |
- readonly response: createMockNetworkLayerQueryResponse; | |
- readonly variables: createMockNetworkLayerQueryVariables; | |
+export type createMockNetworkLayerTestQuery = { | |
+ readonly response: createMockNetworkLayerTestQueryResponse; | |
+ readonly variables: createMockNetworkLayerTestQueryVariables; | |
}; | |
/* | |
-query createMockNetworkLayerQuery( | |
- $artworkID: String! | |
-) { | |
- artwork(id: $artworkID) { | |
- title | |
+query createMockNetworkLayerTestQuery { | |
+ artwork(id: "untitled") { | |
__id | |
+ title | |
} | |
} | |
*/ | |
const node: ConcreteRequest = (function(){ | |
var v0 = [ | |
- { | |
- "kind": "LocalArgument", | |
- "name": "artworkID", | |
- "type": "String!", | |
- "defaultValue": null | |
- } | |
-], | |
-v1 = [ | |
{ | |
"kind": "LinkedField", | |
"alias": null, | |
"name": "artwork", | |
- "storageKey": null, | |
+ "storageKey": "artwork(id:\"untitled\")", | |
"args": [ | |
{ | |
- "kind": "Variable", | |
+ "kind": "Literal", | |
"name": "id", | |
- "variableName": "artworkID", | |
+ "value": "untitled", | |
"type": "String!" | |
} | |
], | |
@@ -56,14 +45,14 @@ v1 = [ | |
{ | |
"kind": "ScalarField", | |
"alias": null, | |
- "name": "title", | |
+ "name": "__id", | |
"args": null, | |
"storageKey": null | |
}, | |
{ | |
"kind": "ScalarField", | |
"alias": null, | |
- "name": "__id", | |
+ "name": "title", | |
"args": null, | |
"storageKey": null | |
} | |
@@ -73,25 +62,25 @@ v1 = [ | |
return { | |
"kind": "Request", | |
"operationKind": "query", | |
- "name": "createMockNetworkLayerQuery", | |
+ "name": "createMockNetworkLayerTestQuery", | |
"id": null, | |
- "text": "query createMockNetworkLayerQuery(\n $artworkID: String!\n) {\n artwork(id: $artworkID) {\n title\n __id\n }\n}\n", | |
+ "text": "query createMockNetworkLayerTestQuery {\n artwork(id: \"untitled\") {\n __id\n title\n }\n}\n", | |
"metadata": {}, | |
"fragment": { | |
"kind": "Fragment", | |
- "name": "createMockNetworkLayerQuery", | |
+ "name": "createMockNetworkLayerTestQuery", | |
"type": "Query", | |
"metadata": null, | |
- "argumentDefinitions": v0, | |
- "selections": v1 | |
+ "argumentDefinitions": [], | |
+ "selections": v0 | |
}, | |
"operation": { | |
"kind": "Operation", | |
- "name": "createMockNetworkLayerQuery", | |
- "argumentDefinitions": v0, | |
- "selections": v1 | |
+ "name": "createMockNetworkLayerTestQuery", | |
+ "argumentDefinitions": [], | |
+ "selections": v0 | |
} | |
}; | |
})(); | |
-(node as any).hash = '77861129af0ff0de2faf595ad7ffa10b'; | |
+(node as any).hash = '7d5663d7e3fdb24e39214db3ed9ea1b7'; | |
export default node; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment