dotansimha / dotansimha/graphql-code-generator-community

Can't get graphql-tag-operations to work with server-side apollo query.

Open
#224 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

### Describe the bug

I have a nextjs project wich requires me to do a server-side query. I have the following **working** code with gql from @apollo/client. It only works like this, but not if I use the gql wrapper from the generated types.

```
// import { gql } from '@/types/gql'
import { gql } from '@apollo/client'
import { useQuery } from '@apollo/client'

export const SEARCH = gql`
query Search($searchTerm: String!) {
search(search: $searchTerm) {
totalResults
results
}
}
```

the server side part (on a nextjs endpoint)

```
const apolloClient = initializeApollo()
try {
const { data } = await apolloClient.query({
query: SEARCH,
variables: {
searchTerm: 'rfid'
}
})
```

(see steps to reproduce for the actual bug)

### Your Example Website or App

Not ready yet.... It's quite a specific setup

### Steps to Reproduce the Bug or Issue

If I want to reproduce the error, I implement the query like this (with gql imported from the generated files instead of apollo-client). It will work fine with useQuery from apollo (clientside), but not with a query executed directly from the apolloClient

```
import { gql } from '@/types/gql'
// import { gql } from '@apollo/client'
import { useQuery } from '@apollo/client'

export const SEARCH = gql(`
query Search($searchTerm: String!) {
search(search: $searchTerm) {
totalResults
results
}
}
`)
```

## The error:
```
Invariant Violation: You must wrap the query string in a "gql" tag.
```

### Expected behavior

I expect to be able to use the `gql` wrapper for writing queries, and still be able to do implement server-side queries with `apollo/graphql`

### Screenshots or Videos

_No response_

### Platform

```
"next": "^12.2.2",
"@apollo/client": "^3.6.9",
"@graphql-codegen/cli": "^2.13.3",
"@graphql-codegen/client-preset": "^1.0.2",
"@graphql-codegen/gql-tag-operations-preset": "^1.6.0",
```

### Codegen Config File

```
import type { CodegenConfig } from '@graphql-codegen/cli'

const config: CodegenConfig = {
overwrite: true,
schema: 'http://localhost:4000',
documents: 'src/**/*.ts',
generates: {
'src/types/gql': {
preset: 'gql-tag-operations-preset',
plugins: []
},
}
}

export default 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.