Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ardatan/graphql-import
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.6.0
Choose a base ref
...
head repository: ardatan/graphql-import
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.7.0
Choose a head ref

Commits on May 24, 2018

  1. Copy the full SHA
    a99a76d View commit details
  2. Copy the full SHA
    692487c View commit details

Commits on May 30, 2018

  1. Copy the full SHA
    8a20d03 View commit details

Commits on Jul 24, 2018

  1. Copy the full SHA
    f7afd63 View commit details
  2. Copy the full SHA
    e44e9ae View commit details

Commits on Jul 27, 2018

  1. Copy the full SHA
    049d67f View commit details
  2. Copy the full SHA
    96b8389 View commit details

Commits on Jul 28, 2018

  1. Copy the full SHA
    ae0dd8c View commit details
  2. Copy the full SHA
    50e912f View commit details

Commits on Aug 1, 2018

  1. Copy the full SHA
    5f82c85 View commit details
  2. test: add test case for collision

    Divyendu Singh committed Aug 1, 2018
    Copy the full SHA
    72ae602 View commit details

Commits on Aug 2, 2018

  1. test: add extra field to collision for clarity

    Divyendu Singh committed Aug 2, 2018
    Copy the full SHA
    87aa832 View commit details

Commits on Aug 3, 2018

  1. Copy the full SHA
    05751fc View commit details

Commits on Aug 7, 2018

  1. Copy the full SHA
    50491cf View commit details

Commits on Aug 10, 2018

  1. Fix nested importing

    Previously only the files that were processed were kept in memory in a Set, to prevent circular dependencies. However, it can happen that a file gets imported two or more times, with different imports. To fix this, instead of only keeping track of the processed files, it now also keeps track of which specific imports from the processed files are used.
    
    The new test I added illustrates this use-case. This might seem like a far stretch, but if you use Prisma and have separated files (e.g. "user.graphql" and "restaurant.graphql", and use each file to import specific types from "generated/prisma.graphql"), you'll walk into this.
    SpaceK33z committed Aug 10, 2018
    Copy the full SHA
    e5cd6e5 View commit details

Commits on Aug 11, 2018

  1. Copy the full SHA
    4923b0f View commit details

Commits on Aug 12, 2018

  1. Copy the full SHA
    8e8b045 View commit details

Commits on Aug 13, 2018

  1. Merge pull request #210 from prismagraphql/test_collision

    test: add test case for collision
    Divyendu Singh authored Aug 13, 2018
    Copy the full SHA
    96bad6d View commit details

Commits on Aug 22, 2018

  1. Copy the full SHA
    d249dd6 View commit details
  2. Copy the full SHA
    b9f7126 View commit details
  3. Copy the full SHA
    88eee71 View commit details

Commits on Aug 23, 2018

  1. Copy the full SHA
    355f3e9 View commit details

Commits on Aug 24, 2018

  1. Copy the full SHA
    6b0399b View commit details

Commits on Aug 25, 2018

  1. Copy the full SHA
    b179a72 View commit details

Commits on Aug 30, 2018

  1. Copy the full SHA
    eac5194 View commit details

Commits on Sep 1, 2018

  1. Merge pull request #213 from SpaceK33z/fix-nested-imports

    fix(nested importing)
    Divyendu Singh authored Sep 1, 2018
    Copy the full SHA
    c336171 View commit details

Commits on Sep 7, 2018

  1. Support for node_modules in graphql imports (#216)

    This is a continuation of PR #136 from @lfades (spoke to @lfades about this and he was okay with it).
    
    It uses `resolve-from` instead of `require.resolve` with the `paths` option because it is not compatible with Node < 8.
    
    Also I think this one works a bit differently; it doesn't introduce a breaking change since it will first try to look up the path relative to the file the import is in, and only if that fails it will use `resolve-from`.
    
    Fixes #57
    SpaceK33z authored Sep 7, 2018
    Copy the full SHA
    4c44f56 View commit details
  2. Copy the full SHA
    e43898c View commit details
  3. feat(modules): Support node_modules paths in graphql imports (#216)

    This is a continuation of PR #136 from @lfades (spoke to @lfades about this and he was okay with it).
    
    It uses `resolve-from` instead of `require.resolve` with the `paths` option because it is not compatible with Node < 8.
    
    Also I think this one works a bit differently; it doesn't introduce a breaking change since it will first try to look up the path relative to the file the import is in, and only if that fails it will use `resolve-from`.
    
    Fixes #57
    SpaceK33z committed Sep 7, 2018
    Copy the full SHA
    c1385af View commit details
105 changes: 28 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

Import &amp; export definitions in GraphQL SDL (also refered to as GraphQL modules)

> There is also a [`graphql-import-loader`](https://github.com/graphcool/graphql-import-loader) for Webpack available.
> There is also a [`graphql-import-loader`](https://github.com/prisma/graphql-import-loader) for Webpack available.
## Install

@@ -24,117 +24,68 @@ const resolvers = {}
const schema = makeExecutableSchema({ typeDefs, resolvers })
```

## Examples

### Importing specific fields

Assume the following directory structure:

```
.
├── a.graphql
├── b.graphql
└── c.graphql
├── schema.graphql
├── posts.graphql
└── comments.graphql
```

`a.graphql`
`schema.graphql`

```graphql
# import B from "b.graphql"
# import Post from "posts.graphql"

type A {
# test 1
first: String
second: Float
b: B
type Query {
posts: [Post]
}
```

`b.graphql`
`posts.graphql`

```graphql
# import C from 'c.graphql'
# import Comment from 'comments.graphql'

type B {
c: C
hello: String!
}
```

`c.graphql`

```graphql
type C {
type Post {
comments: [Comment]
id: ID!
text: String!
tags: [String]
}
```

Running `console.log(importSchema('a.graphql'))` produces the following output:
`comments.graphql`

```graphql
type A {
first: String
second: Float
b: B
}

type B {
c: C
hello: String!
}

type C {
type Comment {
id: ID!
text: String!
}
```

### Extending root fields

It is possible to import from a root field like `Query`;

`queries.graphql`
Running `console.log(importSchema('schema.graphql'))` produces the following output:

```graphql
type Query {
feed: [Post!]!
drafts: [Post!]!
posts: [Post]
}
```

`mutations.graphql`

```graphql
type Mutation {
publish(id: ID!): Post!
deletePost(id: ID!): Post!
}
```

`schema.graphql`

```graphql
# import Query.* from "queries.graphql"
# import Mutation.publish from "mutations.graphql"
```

Running `console.log(importSchema('schema.graphql'))` produces the following output:

```graphql
type Query {
feed: [Post!]!
drafts: [Post!]!
type Post {
comments: [Comment]
id: ID!
text: String!
tags: [String]
}

type Mutation {
publish(id: ID!): Post!
type Comment {
id: ID!
text: String!
}
```

Please refer to [`src/index.test.ts`](https://github.com/graphcool/graphql-import/blob/master/src/index.test.ts) for more examples.

## Development

The [implementation documentation](https://graphql-import.now.sh/) documents how things are implemented under the hood. You can also use the VSCode test setup to debug your code/tests.
## [Full documentation](https://oss.prisma.io/content/graphql-import/overview)

## Related topics & next steps

6 changes: 6 additions & 0 deletions fixtures/collision/a.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# import * from "b.graphql"

type User {
id: ID!
name: String!
}
5 changes: 5 additions & 0 deletions fixtures/collision/b.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type User {
id: ID!
name: String
intro: String
}
6 changes: 6 additions & 0 deletions fixtures/import-module/a.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# import B from 'graphql-import-test/b.graphql'

type A {
id: ID!
author: B!
}
3 changes: 3 additions & 0 deletions fixtures/import-nested/a.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# import Query.first, Query.second from "c.graphql"

type Query
4 changes: 4 additions & 0 deletions fixtures/import-nested/all.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# import Query.first, Query.second from "a.graphql"
# import Query.third from "b.graphql"

type Query
3 changes: 3 additions & 0 deletions fixtures/import-nested/b.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# import Query.third from "c.graphql"

type Query
6 changes: 6 additions & 0 deletions fixtures/import-nested/c.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Query {
first: String
second: Float
third: String
unused: String
}
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -39,20 +39,22 @@
},
"devDependencies": {
"@types/graphql": "0.12.6",
"@types/lodash": "4.14.109",
"@types/node": "9.6.18",
"@types/lodash": "4.14.116",
"@types/resolve-from": "0.0.18",
"@types/node": "9.6.31",
"ava": "0.25.0",
"ava-ts": "0.24.5",
"ava-ts": "0.25.1",
"graphql": "0.13.2",
"nyc": "11.8.0",
"tap-xunit": "2.3.0",
"ts-node": "6.0.3",
"tslint": "5.10.0",
"ts-node": "7.0.1",
"tslint": "5.11.0",
"tslint-config-standard": "7.0.0",
"typedoc": "0.11.1",
"typescript": "2.8.3"
"typescript": "3.0.1"
},
"dependencies": {
"lodash": "^4.17.4"
"lodash": "^4.17.4",
"resolve-from": "^4.0.0"
}
}
63 changes: 63 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test from 'ava'
import * as fs from 'fs'
import { parseImportLine, parseSDL, importSchema } from '.'

test('parseImportLine: parse single import', t => {
@@ -44,6 +45,13 @@ test('parseImportLine: different path', t => {
})
})

test('parseImportLine: module in node_modules', t => {
t.deepEqual(parseImportLine(`import A from "module-name"`), {
imports: ['A'],
from: 'module-name',
})
})

test('parseSDL: non-import comment', t => {
t.deepEqual(parseSDL(`#importent: comment`), [])
})
@@ -65,6 +73,39 @@ test('parse: multi line import', t => {
])
})

test('Module in node_modules', t => {
const b = `\
# import lower from './lower.graphql'
type B {
id: ID!
nickname: String! @lower
}
`
const lower = `\
directive @lower on FIELD_DEFINITION
`
const expectedSDL = `\
type A {
id: ID!
author: B!
}
type B {
id: ID!
nickname: String! @lower
}
directive @lower on FIELD_DEFINITION
`
const moduleDir = 'node_modules/graphql-import-test'
if (!fs.existsSync(moduleDir)) {
fs.mkdirSync(moduleDir)
}
fs.writeFileSync(moduleDir + '/b.graphql', b)
fs.writeFileSync(moduleDir + '/lower.graphql', lower)
t.is(importSchema('fixtures/import-module/a.graphql'), expectedSDL)
})

test('importSchema: imports only', t => {
const expectedSDL = `\
type Query {
@@ -87,6 +128,17 @@ type Query {
t.is(importSchema('fixtures/import-duplicate/all.graphql'), expectedSDL)
})

test('importSchema: import nested', t => {
const expectedSDL = `\
type Query {
first: String
second: Float
third: String
}
`
t.is(importSchema('fixtures/import-nested/all.graphql'), expectedSDL)
})

test('importSchema: field types', t => {
const expectedSDL = `\
type A {
@@ -744,3 +796,14 @@ test('missing type on directive', t => {
`Directive first: Couldn't find type first in any of the schemas.`,
)
})

test('import with collision', t => {
// Local type gets preference over imported type
const expectedSDL = `\
type User {
id: ID!
name: String!
}
`
t.is(importSchema('fixtures/collision/a.graphql'), expectedSDL)
})
Loading