dotansimha / dotansimha/graphql-code-generator

Escape characters within a string cause an error in client preset

Open
#9,863 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Which packages are impacted by your issue?

@graphql-codegen/client-preset

### Describe the bug

Hey!

I'm using the client preset and I'm running into an error with a query like this:

```gql
query user {
user(id: 1, query: "{\"$orderby\":{\"username\":\"asc\"}}") {
id
username
email
}
}
```

The output is as follows:
```
√ Parse Configuration
‼ Generate outputs
> Generate to ./src/schemas/generated/
√ Load GraphQL schemas
√ Load GraphQL documents
× Syntax Error: Expected Name, found $

```

I've provided a minimal reproduction here:

https://stackblitz.com/edit/github-odqmc9?file=package.json

The problem doesn't seem to be the dollar sign though, but escaped strings in general. When removing the dollar sign, the error message changes to:

```
√ Parse Configuration
‼ Generate outputs
> Generate to ./src/schemas/generated/
√ Load GraphQL schemas
√ Load GraphQL documents
× Syntax Error: Cannot parse the unexpected character "\\".
```

**Workaround**

During writing of this bug report I found the following workaround:

Changing the string from `"{\"$orderby\":{\"username\":\"asc\"}}"` to `"""{"$orderby":{"lastname":"asc", "firstname":"asc"}}"""` does not cause the error.

### Your Example Website or App

https://stackblitz.com/edit/github-odqmc9?file=package.json

### Steps to Reproduce the Bug or Issue

run `yarn generate`

### Expected behavior

As a user, I expect that an escaped double quotation in a string is processed correctly

### Screenshots or Videos

_No response_

### Platform

- OS: Windows
- NodeJS: 18.18.2
- `graphql` version: ^16.2.0
- `@graphql-codegen/add`: ^5.0.0
- `@graphql-codegen/cli`: ^4.0.1
- `@graphql-codegen/typescript`: 4.0.1
- `@graphql-codegen/typescript-operations`: 4.0.1
- `@graphql-codegen/client-preset`: 4.2.3

### Codegen Config File

```typescript
const config: CodegenConfig = {
schema: 'schema.graphql',
documents: 'document.graphql',
generates: {
'src/gql/': { preset: 'client' },
},
};
```

### 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.