An NPM plugin to fetch data from an arbitrary GraphQL data source
- You will need a set of GraphQL queries in one or more files in a specified folder
- If you have multiple files with queries that need to be run, you must import the queries into an index.ts file
- An index.ts files is required - an example is given below
- You will need an output directory where the retrieved data will be stored
- You will need to name your query call to the GraphQL endpoint
- You will need to full URL to your GraphQL endpoint
- Globally install typescript
- There are several node modules listed in the dev dependencies list that should get installed locally - those are required for correct TypeScript type identification
- If you are using this with Gatsby you will need to add parameters to your gatsby-config.ts file
How to use (in gatsby-config.ts
):
include * from './content/graphql'
module.exports = {
plugins: [
...
{
resolve: '@ndlib/graphql-harvest',
options: {
query_name: <any-name-you-want>,
graphql_endpoint: <full-url-to-graphql-endpoint>,
query_types: <included-module-with-graphql-queries-present>,
results_output_path: <path-to-output-folder-where-results-are-stored>
}
},
...
]
}
Example of index.ts file that assigned to the query_types
folder:
export * as people from './people'
export * as subjects from './subjects'
export * as teams from './teams'
export * as units from './units'
export * as universityCommittees from './universityCommittees'
Each of the types referenced above will have a GraphQL query in it and will be included at harvest time. A corresponding JSON file will be created in the output folder named for the type it represents. For example, in the above index.ts file, a people.json file will be created upon completion of the harvest assuming that the query is syntactically correct and the endpoint provides data matching the query.
Parameter | Description | Required |
---|---|---|
query_name | An arbitrary that will get passed to the endpoint which can be used to track query execution in the endpoint log files | yes |
graphql_endpoint | The full URL to the GraphQL endpoint | yes |
query_types | Included at the top of the gatsby_config.ts file - an index.(js|ts) file which includes all of the types being queried | yes |
results_output_path | The directory where the output files will be written - one JSON file per type included in the index.(js|ts) file | yes |