Repository: kamilkisiela/graphql-inspector
Pull request #434 (2019-07-18 12:19:01)

load: make sure we cover few use-cases


Patch:
diff --git a/packages/load/samples/schema/export-document-node.ts b/packages/load/samples/schema/export-document-node.ts
new file mode 100644
index 00000000..66fb6ce1
--- /dev/null
+++ b/packages/load/samples/schema/export-document-node.ts
@@ -0,0 +1,7 @@
+import {parse} from 'graphql';
+
+export default parse(`
+  type Query {
+    test: String
+  }
+`);
diff --git a/packages/load/samples/schema/export-schema-object.ts b/packages/load/samples/schema/export-schema-object.ts
new file mode 100644
index 00000000..f88d8135
--- /dev/null
+++ b/packages/load/samples/schema/export-schema-object.ts
@@ -0,0 +1,7 @@
+import {buildSchema} from 'graphql';
+
+export default buildSchema(`
+  type Query {
+    test: String
+  }
+`);
diff --git a/packages/load/samples/schema/export-string-in-code.ts b/packages/load/samples/schema/export-string-in-code.ts
new file mode 100644
index 00000000..66df8b3a
--- /dev/null
+++ b/packages/load/samples/schema/export-string-in-code.ts
@@ -0,0 +1,6 @@
+import {readFileSync} from 'fs';
+import {resolve} from 'path';
+
+export default readFileSync(resolve(__dirname, './schema.graphql'), {
+  encoding: 'utf-8',
+});
diff --git a/packages/load/samples/schema/export-string.ts b/packages/load/samples/schema/export-string.ts
new file mode 100644
index 00000000..6b14cdf9
--- /dev/null
+++ b/packages/load/samples/schema/export-string.ts
@@ -0,0 +1,5 @@
+export default `
+  type Query {
+    test: String
+  }
+`;
diff --git a/packages/load/samples/schema/graphql/foo.graphql b/packages/load/samples/schema/graphql/foo.graphql
new file mode 100644
index 00000000..76d1f196
--- /dev/null
+++ b/packages/load/samples/schema/graphql/foo.graphql
@@ -0,0 +1,3 @@
+type Foo {
+  test: String
+}
diff --git a/packages/load/samples/schema/graphql/query.graphql b/packages/load/samples/schema/graphql/query.graphql
new file mode 100644
index 00000000..916de7ee
--- /dev/null
+++ b/packages/load/samples/schema/graphql/query.graphql
@@ -0,0 +1,3 @@
+type Query {
+  foo: Foo
+}
diff --git a/packages/load/samples/schema/schema.graphql b/packages/load/samples/schema/schema.graphql
new file mode 100644
index 00000000..c49a0c7b
--- /dev/null
+++ b/packages/load/samples/schema/schema.graphql
@@ -0,0 +1,3 @@
+type Query {
+  test: String
+}
diff --git a/packages/load/samples/schema/type-defs/foo.ts b/packages/load/samples/schema/type-defs/foo.ts
new file mode 100644
index 00000000..b82b540a
--- /dev/null
+++ b/packages/load/samples/schema/type-defs/foo.ts
@@ -0,0 +1,7 @@
+import gql from 'graphql-tag';
+
+const foo = gql`
+  type Foo {
+    test: String
+  }
+`;
diff --git a/packages/load/samples/schema/type-defs/query.ts b/packages/load/samples/schema/type-defs/query.ts
new file mode 100644
index 00000000..dd27ed22
--- /dev/null
+++ b/packages/load/samples/schema/type-defs/query.ts
@@ -0,0 +1,7 @@
+import gql from 'graphql-tag';
+
+const query = gql`
+  type Query {
+    foo: Foo
+  }
+`;
diff --git a/packages/load/samples/schema/with-graphql-tag.ts b/packages/load/samples/schema/with-graphql-tag.ts
new file mode 100644
index 00000000..26a7631b
--- /dev/null
+++ b/packages/load/samples/schema/with-graphql-tag.ts
@@ -0,0 +1,7 @@
+import gql from 'graphql-tag';
+
+const typeDefs = gql`
+  type Query {
+    test: String
+  }
+`;


Test patch:
diff --git a/packages/load/__tests__/integration.ts b/packages/load/__tests__/integration.ts
new file mode 100644
index 00000000..3f6c7a31
--- /dev/null
+++ b/packages/load/__tests__/integration.ts
@@ -0,0 +1,82 @@
+import {GraphQLSchema, printSchema, buildSchema} from 'graphql';
+import {loadSchema} from '../src';
+
+const SAMPLES_DIR = `samples`;
+
+function matchSchema(schema: GraphQLSchema) {
+  expect(printSchema(schema)).toBe(
+    printSchema(
+      buildSchema(/* GraphQL */ `
+        type Query {
+          test: String
+        }
+      `),
+    ),
+  );
+}
+
+function matchCombinedSchema(schema: GraphQLSchema) {
+  expect(printSchema(schema)).toBe(
+    printSchema(
+      buildSchema(/* GraphQL */ `
+        type Foo {
+          test: String
+        }
+
+        type Query {
+          foo: Foo
+        }
+      `),
+    ),
+  );
+}
+
+describe('load schema', () => {
+  test('export GraphQLSchemaObject', async () => {
+    const schema = await loadSchema(
+      `${SAMPLES_DIR}/schema/export-schema-object.ts`,
+    );
+    matchSchema(schema);
+  });
+
+  test('export DocumentNode', async () => {
+    const schema = await loadSchema(
+      `${SAMPLES_DIR}/schema/export-document-node.ts`,
+    );
+    matchSchema(schema);
+  });
+
+  test('export string', async () => {
+    const schema = await loadSchema(`${SAMPLES_DIR}/schema/export-string.ts`);
+    matchSchema(schema);
+  });
+
+  test('export string (in code)', async () => {
+    const schema = await loadSchema(
+      `${SAMPLES_DIR}/schema/export-string-in-code.ts`,
+    );
+    matchSchema(schema);
+  });
+
+  test('one .graphql file', async () => {
+    const schema = await loadSchema(`${SAMPLES_DIR}/schema/schema.graphql`);
+    matchSchema(schema);
+  });
+
+  test('multiple .graphql files', async () => {
+    const schema = await loadSchema(`${SAMPLES_DIR}/schema/graphql/*.graphql`);
+    matchCombinedSchema(schema);
+  });
+
+  test('multiple .ts files', async () => {
+    const schema = await loadSchema(`${SAMPLES_DIR}/schema/type-defs/*.ts`);
+    matchCombinedSchema(schema);
+  });
+
+  test('graphql-tag', async () => {
+    const schema = await loadSchema(
+      `${SAMPLES_DIR}/schema/with-graphql-tag.ts`,
+    );
+    matchSchema(schema);
+  });
+});