dotansimha / dotansimha/graphql-code-generator

addToSchema not working

Open
#8,552 1 comment 0 reactions 0 assignees View on GitHub
core
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Describe the bug

I'm trying to write a plugin that supplies several scalars, directives, and interfaces via `addToSchema`. When I run the plugin against a sample file, however, it exits with an error because the `addToSchema` content is missing.

### Your Example Website or App

https://github.com/ianwremmel/data/pull/4

### Steps to Reproduce the Bug or Issue

1. Clone and checkout the branch above
2. `npm install`
3. `make clean build` (or `npx graphql-codegen --project user-session --debug`)

The relevant files are pasted below

```ts
// plugin
import {readFileSync} from 'fs';
import path from 'path';

import {
AddToSchemaResult,
PluginFunction,
} from '@graphql-codegen/plugin-helpers';

import {CloudformationPluginConfig} from './config';

/** @override */
export function addToSchema(): AddToSchemaResult {
return readFileSync(path.resolve(__dirname, '../schema.graphqls'), 'utf8');
}

/** @override */
export const plugin: PluginFunction = (
schema,
documents,
config,
info
) => {
return `It Works!${Date.now()}`;
};
```

```graphql
# schema.graphqls
"""
JavaScript Date stored as a Number in DynamoDB
"""
scalar Date

"""
Arbitrary JSON stored as a Map in DynamoDB
"""
scalar JSONObject

"""
Indicates all reads for this type will be done using DynamoDB's
strong-consistency mode. General discouraged, though certain user-facing flows
might require it.
"""
directive @consistent on OBJECT

"""
Indicates the field will be configured as the DynamoDB partition key for this
entity. Requires a simple key schema.
"""
directive @primary on FIELD_DEFINITION

"""
Indicates the field will be configured as the DynamoDB ttl field for this type.
Always gets saved to a column named "ttl" to avoid conflicts in single-table
entities that all might use different names
"""
directive @ttl(duration: String!) on FIELD_DEFINITION

"""
SimpleModels are DynamoDB with a key schema that does not include a sort key.
"""
interface SimpleModel {
id: ID!
}
```

```
# fixture.graphqls
"""
A user session object.
"""
type UserSession implements Node & SimpleModel @consistent {
expires: Date! @ttl(duration: "1d")
id: ID! @primary
session: JSONObject!
}
```

### Expected behavior

As a user, I expect graphql-codegen to exit 0, rather than printing the following

```
npx graphql-codegen --project user-session --debug
(node:52757) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
✔ Parse Configuration
⚠ Generate outputs
❯ Generate to examples/user-session/__generated__/template.yml
✖ Unknown type: "SimpleModel".
◼ Load GraphQL documents
◼ Generate
Running lifecycle hook "afterStart" scripts...
[CLI] Loading Schemas
```

### Screenshots or Videos

_No response_

### Platform

- OS: maxOS
- NodeJS: v16.13.0
- `graphql` version: 16.6.0
- `@graphql-codegen/plugin-helpers`: 2.7.1
- `@graphql-codegen/cli`: 2.13.8

### Codegen Config File

```js
//.graphqlrc
const {sync: glob} = require('glob');

const examples = glob('*/', {cwd: 'examples'}).map((pathName) =>
pathName.replace(/\/$/, '')
);

/** @type {import('graphql-config').IGraphQLConfig } */
const config = {
projects: examples.reduce((acc, example) => {
acc[example] = {
extensions: {
codegen: {
generates: {
[`examples/${example}/__generated__/template.yml`]: {
plugins: ['./src/codegen/cloudformation'],
},
},
},
},
schema: [
'examples/common.graphqls',
// 'src/codegen/schema.graphqls',
`examples/${example}/schema/**/*.graphqls`,
],
};
return acc;
}, {}),
};

module.exports = config;
```

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.