Skip to content

Commit b07d2ab

Browse files
n1ru4lardatan
andauthored
docs: update documents-loading.mdx (#4686)
* Update documents-loading.mdx * run prettier * add up2date link * Add tests for document strings Co-authored-by: Arda TANRIKULU <[email protected]>
1 parent 0cac320 commit b07d2ab

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

packages/load/tests/loaders/documents/documents-from-glob.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,11 @@ describe('documentsFromGlob', () => {
186186
expect(result).toHaveLength(1);
187187
expect(result[0].rawSDL).toContain(pointerOptions.fooFieldName);
188188
});
189+
test('should work only with a document string', async () => {
190+
const result = await load('query { foo }', {
191+
loaders: [],
192+
});
193+
expect(result).toHaveLength(1);
194+
});
189195
});
190196
});

website/docs/documents-loading.mdx

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Any input provided as a source will be recognized by utils automatically.
1111

1212
It also extracts usages of `gql` from code files using [`@graphql-tools/graphql-tag-pluck`](/docs/graphql-tag-pluck).
1313

14-
For notes on typescript, refer to [loaders](/docs/loaders)
14+
For notes on typescript, refer to [Schema Loading > Loaders](/docs/schema-loading#loaders)
1515

1616
## Usage
1717

@@ -21,7 +21,9 @@ const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader')
2121
const { CodeFileLoader } = require('@graphql-tools/code-file-loader')
2222

2323
// load from string
24-
const document1 = loadDocuments('query { f }')
24+
const document1 = loadDocuments('query { f }', {
25+
loaders: []
26+
})
2527

2628
// load from a single file
2729
const document2 = loadDocuments('./users.query.graphql', {
@@ -52,3 +54,52 @@ interface DocumentSource {
5254
`loadDocuments` takes in additional configuration via the `options` object (the second argument). There are some defaults to be aware of - to learn more, see [the full API documentation](/docs/api/modules/load/#loaddocuments).
5355

5456
> You can learn more about [loaders](/docs/loaders) to load documents from different sources.
57+
58+
## Error handling
59+
60+
### No files found
61+
62+
In case no documents were found and loaded via the provided glob an exception is thrown.
63+
64+
```ts
65+
import { loadDocuments } from '@graphql-tools/load'
66+
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
67+
68+
const documents = await loadDocuments(['i-do-not-exist.graphql'], {
69+
loaders: [new GraphQLFileLoader()]
70+
})
71+
```
72+
73+
If `i-do-not-exist.graphql` does not exist this throws the following error:
74+
75+
```
76+
Error:
77+
Unable to find any GraphQL type definitions for the following pointers:
78+
79+
- *.graphql
80+
```
81+
82+
### Syntax error within a file
83+
84+
In case you try to load a GraphQL file with invalid syntax, the `loadDocuments` function will throw a `GraphQLError`.
85+
86+
You can identify the exact file that caused this GraphQLError by using the errors source property.
87+
88+
```ts
89+
import { loadDocuments } from '@graphql-tools/load'
90+
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
91+
import { CodeFileLoader } from '@graphql-tools/code-file-loader'
92+
import { GraphQLError } from 'graphql'
93+
94+
try {
95+
const documents = await loadDocuments(['packages/**/*.graphql', 'packages/**/*.ts(x)'], {
96+
loaders: [new GraphQLFileLoader(), new CodeFileLoader()]
97+
})
98+
console.log(documents)
99+
} catch (err) {
100+
if (err instanceof GraphQLError) {
101+
console.log(err.message + `\n in ${err.source?.name}`)
102+
}
103+
throw err
104+
}
105+
```

0 commit comments

Comments
 (0)