Unverified Commit 04c99067 authored by Soichi Takamura's avatar Soichi Takamura Committed by GitHub

Update with-typescript-graphql (#16101)

parent 624b748d
{
"presets": ["next/babel"]
}
...@@ -33,6 +33,7 @@ yarn-error.log* ...@@ -33,6 +33,7 @@ yarn-error.log*
# vercel # vercel
.vercel .vercel
# graphql # graphql-let
__generated__
*.graphql.d.ts *.graphql.d.ts
*.graphqls.d.ts *.graphqls.d.ts
schema: lib/type-defs.graphqls schema: '**/*.graphqls'
schemaEntrypoint: 'lib/type-defs.graphqls'
documents: '**/*.graphql' documents: '**/*.graphql'
plugins: plugins:
- typescript - typescript
- typescript-operations - typescript-operations
- typescript-react-apollo - typescript-react-apollo
respectGitIgnore: true cacheDir: __generated__
module.exports = {
roots: ['<rootDir>'],
moduleFileExtensions: ['js', 'ts', 'tsx', 'json'],
testPathIgnorePatterns: ['<rootDir>[/\\\\](node_modules|.next)[/\\\\]'],
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(ts|tsx)$'],
transform: {
'^.+\\.(ts|tsx)$': 'babel-jest',
'\\.graphql$': [
'graphql-let/jestTransformer',
{ subsequentTransformer: 'babel-jest' },
],
},
}
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
import { useMemo } from 'react' import { useMemo } from 'react'
import { ApolloClient } from 'apollo-client' import {
import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory' ApolloClient,
InMemoryCache,
NormalizedCacheObject,
} from '@apollo/client'
let apolloClient: ApolloClient<NormalizedCacheObject> | undefined let apolloClient: ApolloClient<NormalizedCacheObject> | undefined
...@@ -12,11 +15,11 @@ export type ResolverContext = { ...@@ -12,11 +15,11 @@ export type ResolverContext = {
function createIsomorphLink(context: ResolverContext = {}) { function createIsomorphLink(context: ResolverContext = {}) {
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
const { SchemaLink } = require('apollo-link-schema') const { SchemaLink } = require('@apollo/client/link/schema')
const { schema } = require('./schema') const { schema } = require('./schema')
return new SchemaLink({ schema, context }) return new SchemaLink({ schema, context })
} else { } else {
const { HttpLink } = require('apollo-link-http') const { HttpLink } = require('@apollo/client')
return new HttpLink({ return new HttpLink({
uri: '/api/graphql', uri: '/api/graphql',
credentials: 'same-origin', credentials: 'same-origin',
......
fragment Partial on User {
id
name
}
import { makeExecutableSchema } from 'graphql-tools' import { join } from 'path'
import typeDefs from './type-defs.graphqls' import { makeExecutableSchema } from '@graphql-tools/schema'
import { loadFilesSync } from '@graphql-tools/load-files'
import { mergeTypeDefs } from '@graphql-tools/merge'
import graphQLLetConfig from '../.graphql-let.yml'
import resolvers from './resolvers' import resolvers from './resolvers'
const loadedFiles = loadFilesSync(join(process.cwd(), graphQLLetConfig.schema))
const typeDefs = mergeTypeDefs(loadedFiles)
export const schema = makeExecutableSchema({ export const schema = makeExecutableSchema({
typeDefs, typeDefs,
resolvers, resolvers,
......
type User {
id: ID!
name: String!
status: String!
}
type Query { type Query {
viewer: User! viewer: User!
} }
type User {
id: ID!
name: String!
status: String!
}
# import Partial from './partial.graphql'
query Viewer { query Viewer {
viewer { viewer {
id ...Partial
name
status status
} }
} }
...@@ -5,3 +5,5 @@ declare module '*.graphqls' { ...@@ -5,3 +5,5 @@ declare module '*.graphqls' {
import { DocumentNode } from 'graphql' import { DocumentNode } from 'graphql'
export default typeof DocumentNode export default typeof DocumentNode
} }
declare module '*.yml'
...@@ -9,7 +9,13 @@ module.exports = { ...@@ -9,7 +9,13 @@ module.exports = {
config.module.rules.push({ config.module.rules.push({
test: /\.graphqls$/, test: /\.graphqls$/,
exclude: /node_modules/, exclude: /node_modules/,
use: ['graphql-tag/loader', 'graphql-let/schema/loader'], use: ['graphql-let/schema/loader'],
})
config.module.rules.push({
test: /\.ya?ml$/,
type: 'json',
use: 'yaml-loader',
}) })
return config return config
......
...@@ -7,36 +7,36 @@ ...@@ -7,36 +7,36 @@
"codegen": "graphql-let", "codegen": "graphql-let",
"dev": "yarn codegen && next", "dev": "yarn codegen && next",
"build": "yarn codegen && next build", "build": "yarn codegen && next build",
"test": "yarn codegen && jest",
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@apollo/react-common": "3.1.4", "@apollo/client": "^3.1.3",
"@apollo/react-components": "^3.1.5", "@graphql-tools/load-files": "6.0.18",
"@apollo/react-hooks": "3.1.5", "@graphql-tools/merge": "6.0.18",
"apollo-cache": "^1.3.5", "@graphql-tools/schema": "6.0.18",
"apollo-cache-inmemory": "^1.6.6", "apollo-server-micro": "^2.16.1",
"apollo-client": "^2.6.10", "graphql": "15.3.0",
"apollo-link": "1.2.14",
"apollo-link-http": "1.5.17",
"apollo-link-schema": "1.2.5",
"apollo-server-micro": "^2.14.2",
"apollo-utilities": "^1.3.3",
"graphql": "^14.6.0",
"graphql-tag": "^2.10.3",
"next": "latest", "next": "latest",
"react": "^16.13.1", "react": "^16.13.1",
"react-dom": "^16.13.1" "react-dom": "^16.13.1"
}, },
"devDependencies": { "devDependencies": {
"@graphql-codegen/cli": "^1.15.1", "@graphql-codegen/cli": "^1.17.8",
"@graphql-codegen/plugin-helpers": "^1.15.1", "@graphql-codegen/plugin-helpers": "^1.17.8",
"@graphql-codegen/typescript": "^1.13.3", "@graphql-codegen/typescript": "^1.17.8",
"@graphql-codegen/typescript-operations": "^1.13.3", "@graphql-codegen/typescript-operations": "^1.17.8",
"@graphql-codegen/typescript-react-apollo": "^1.13.3", "@graphql-codegen/typescript-react-apollo": "^2.0.6",
"@graphql-codegen/typescript-resolvers": "^1.15.1", "@graphql-codegen/typescript-resolvers": "^1.17.8",
"@types/react": "^16.9.34", "@types/react": "^16.9.46",
"@types/react-dom": "^16.9.7", "@types/react-dom": "^16.9.8",
"@types/react-test-renderer": "16.9.3",
"babel-jest": "26.3.0",
"graphql-let": "0.x", "graphql-let": "0.x",
"typescript": "^3.8.3" "graphql-tag": "2.11.0",
"jest": "26.4.0",
"react-test-renderer": "16.13.1",
"typescript": "^3.9.7",
"yaml-loader": "0.6.0"
} }
} }
import { AppProps } from 'next/app' import { AppProps } from 'next/app'
import { ApolloProvider } from '@apollo/react-hooks' import { ApolloProvider } from '@apollo/client'
import { useApollo } from '../lib/apollo' import { useApollo } from '../lib/apollo'
export default function App({ Component, pageProps }: AppProps) { export default function App({ Component, pageProps }: AppProps) {
......
...@@ -3,8 +3,7 @@ import { useViewerQuery, ViewerDocument } from '../lib/viewer.graphql' ...@@ -3,8 +3,7 @@ import { useViewerQuery, ViewerDocument } from '../lib/viewer.graphql'
import { initializeApollo } from '../lib/apollo' import { initializeApollo } from '../lib/apollo'
const Index = () => { const Index = () => {
const { data } = useViewerQuery() const { viewer } = useViewerQuery().data!
const { viewer } = data!
return ( return (
<div> <div>
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Index renders the html we want 1`] = `
<div>
You're signed in as
Baa
and you're
Healthy
go to the
<a
href="/about"
onClick={[Function]}
onMouseEnter={[Function]}
>
about
</a>
page.
</div>
`;
import { InMemoryCache, gql } from '@apollo/client'
import React from 'react'
import Index from '../pages'
import renderer from 'react-test-renderer'
import { MockedProvider } from '@apollo/client/testing'
const cache = new InMemoryCache()
cache.writeQuery({
query: gql`
query Viewer {
viewer {
id
name
status
}
}
`,
data: {
viewer: {
__typename: 'User',
id: 'Baa',
name: 'Baa',
status: 'Healthy',
},
},
})
describe('Index', () => {
it('renders the html we want', async () => {
const component = renderer.create(
<MockedProvider cache={cache}>
<Index />
</MockedProvider>
)
expect(component.toJSON()).toMatchSnapshot()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment