Skip to content

Commit

Permalink
Merge pull request #88 from brandonchinn178/test-codegen
Browse files Browse the repository at this point in the history
Fix ambiguous field name error
  • Loading branch information
brandonchinn178 authored Jan 9, 2024
2 parents 64150bc + 10ae5d4 commit f67d1e1
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 44 deletions.
51 changes: 38 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,6 @@ jobs:
files: lcov.info,lcov-overlay.info

# Build + test example
-
name: Install fourmolu
run: |
curl -fsSL \
"https://github.com/fourmolu/fourmolu/releases/download/v${FOURMOLU_VERSION}/fourmolu-${FOURMOLU_VERSION}-linux-x86_64" \
-o /usr/local/bin/fourmolu
chmod +x /usr/local/bin/fourmolu
env:
FOURMOLU_VERSION: '0.13.0.0'
-
name: Test that example API is up to date
run: |
Expand Down Expand Up @@ -106,6 +97,36 @@ jobs:
steps:
-
uses: actions/checkout@v3

# https://github.com/actions/setup-node/issues/904
-
uses: actions/setup-node@v3
with:
node-version: latest
-
run: corepack enable

# Build JS
-
uses: actions/setup-node@v3
with:
node-version: latest
cache: yarn
-
uses: actions/cache@v3
with:
path: ~/.stack
key: ${{ runner.os }}-stack-cache-${{ hashFiles('stack.yaml', 'package.yaml') }}
-
name: Build graphql-codegen-haskell
run: yarn install --immutable && yarn build
-
uses: actions/upload-artifact@v3
with:
name: graphql-codegen-haskell.js
path: graphql-client/js/graphql-codegen-haskell.js

# Build + Test Cabal
-
id: setup
name: Set up GHC ${{ matrix.ghc_version }}
Expand All @@ -114,8 +135,11 @@ jobs:
ghc-version: ${{ matrix.ghc_version }}
-
name: Configure the build
run: |
cabal configure --enable-test --test-show-details=streaming
run: >
cabal configure
--enable-test
--test-show-details=streaming
--ghc-options '-Wno-unused-packages'
-
if: ${{ matrix.oldest }}
name: Use oldest dependencies
Expand All @@ -137,8 +161,9 @@ jobs:
restore-keys: |
${{ runner.os }}-cabal-cache-${{ env.CURR_MONTH }}-${{ matrix.ghc_version }}-
-
name: Build + Test
run: cabal test all
run: cabal build all
-
run: cabal exec cabal test all
-
name: Check that files are unmodified
run: git add -A && git diff --staged --exit-code
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repos:
entry: fourmolu -i
language: system
files: '\.hs$'
exclude: 'Example/GraphQL/(API.hs|Enums/)'

# Typescript
- repo: local
Expand Down
4 changes: 0 additions & 4 deletions example/scripts/codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ set -eux -o pipefail
ROOT="$(builtin cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

stack exec -- graphql-codegen -c "${ROOT}/codegen.yml"

fourmolu -i \
"${ROOT}"/src/Example/GraphQL/API.hs \
"${ROOT}"/src/Example/GraphQL/Enums/*.hs
20 changes: 10 additions & 10 deletions example/src/Example/GraphQL/API.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{- This file was automatically generated and should not be edited. -}

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -w #-}

Expand All @@ -11,8 +13,8 @@ module Example.GraphQL.API where
import Data.GraphQL
import Data.GraphQL.Bootstrap

import Example.GraphQL.Enums.ReleaseStatus
import Example.GraphQL.Scalars
import Example.GraphQL.Enums.ReleaseStatus

{-----------------------------------------------------------------------------
-- getRecordings
Expand All @@ -36,8 +38,7 @@ data GetRecordingsQuery = GetRecordingsQuery
}
deriving (Show)

type GetRecordingsSchema =
[schema|
type GetRecordingsSchema = [schema|
{
search: Maybe {
recordings: Maybe {
Expand Down Expand Up @@ -72,8 +73,7 @@ instance GraphQLQuery GetRecordingsQuery where

getQueryName _ = "getRecordings"

getQueryText _ =
[query|
getQueryText _ = [query|
query getRecordings($query: String!, $first: Int) {
search {
recordings(query: $query, first: $first) {
Expand Down Expand Up @@ -103,8 +103,8 @@ instance GraphQLQuery GetRecordingsQuery where
}
|]

getArgs query =
object
[ "query" .= _query (query :: GetRecordingsQuery)
, "first" .= _first (query :: GetRecordingsQuery)
]
getArgs GetRecordingsQuery{..} = object
[ "query" .= _query
, "first" .= _first
]

3 changes: 1 addition & 2 deletions example/src/Example/GraphQL/Enums/ReleaseStatus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module Example.GraphQL.Enums.ReleaseStatus where

import Data.GraphQL.Bootstrap

mkEnum
"ReleaseStatus"
mkEnum "ReleaseStatus"
[ "OFFICIAL"
, "PROMOTION"
, "BOOTLEG"
Expand Down
4 changes: 4 additions & 0 deletions graphql-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# v1.2.4

* Fix ambiguous field name error with same argument name in multiple queries ([#86](https://github.com/brandonchinn178/graphql-client/issues/86))

# v1.2.3

* Add support for GHC 9.8
Expand Down
12 changes: 10 additions & 2 deletions graphql-client/graphql-client.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: >= 1.10
-- see: https://github.com/sol/hpack

name: graphql-client
version: 1.2.3
version: 1.2.4
synopsis: A client for Haskell programs to query a GraphQL API
description: A client for Haskell programs to query a GraphQL API.
category: Graphql
Expand All @@ -19,6 +19,12 @@ build-type: Simple
extra-source-files:
README.md
CHANGELOG.md
test/Data/GraphQL/Test/Generation/run.sh
test/Data/GraphQL/Test/Generation/src/Example/Scalars.hs
test/Data/GraphQL/Test/Generation/queries/basic.graphql
test/Data/GraphQL/Test/Generation/queries/duplicate-names.graphql
test/Data/GraphQL/Test/Generation/schema.graphql
test/Data/GraphQL/Test/Generation/codegen.yml
./js/graphql-codegen-haskell.js
./js/graphql-codegen-haskell-fallback.js
./js/mocks/__utils.js
Expand Down Expand Up @@ -68,7 +74,7 @@ executable graphql-codegen
, file-embed <0.1
, optparse-applicative <0.19
, path <0.10
, path-io <2
, path-io >=1.3.3 && <2
, template-haskell <3
, typed-process <0.3
default-language: Haskell2010
Expand All @@ -77,6 +83,7 @@ test-suite graphql-client-test
type: exitcode-stdio-1.0
main-is: Main.hs
other-modules:
Data.GraphQL.Test.Generation.TestGeneration
Data.GraphQL.Test.Monad.Class
Data.GraphQL.Test.TestQuery
Data.GraphQL.Test.TestUtils
Expand All @@ -90,6 +97,7 @@ test-suite graphql-client-test
, base
, graphql-client
, mtl
, process
, tasty
, tasty-hunit
default-language: Haskell2010
10 changes: 8 additions & 2 deletions graphql-client/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: graphql-client
version: 1.2.3
version: 1.2.4
verbatim:
cabal-version: '>= 1.10'
license: BSD3
Expand All @@ -14,6 +14,11 @@ description: A client for Haskell programs to query a GraphQL API.
extra-source-files:
- README.md
- CHANGELOG.md
# test generation files
- test/Data/GraphQL/Test/Generation/run.sh
- test/Data/GraphQL/Test/Generation/src/**/*
- test/Data/GraphQL/Test/Generation/**/*.graphql
- test/Data/GraphQL/Test/Generation/codegen.yml
# Make sure graphql-codegen-haskell.js is listed in the Cabal file,
# in the off-chance it's not built yet
- ./js/graphql-codegen-haskell.js
Expand Down Expand Up @@ -53,7 +58,7 @@ executables:
- file-embed < 0.1
- optparse-applicative < 0.19
- path < 0.10
- path-io < 2
- path-io >= 1.3.3 && < 2
- template-haskell < 3
- typed-process < 0.3

Expand All @@ -67,5 +72,6 @@ tests:
- base
- graphql-client
- mtl
- process
- tasty
- tasty-hunit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gen/
18 changes: 18 additions & 0 deletions graphql-client/test/Data/GraphQL/Test/Generation/TestGeneration.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Data.GraphQL.Test.Generation.TestGeneration (testGeneration) where

import System.Exit (ExitCode (..))
import System.Process (readProcessWithExitCode)
import Test.Tasty
import Test.Tasty.HUnit

testGeneration :: TestTree
testGeneration =
testCase "Test generated code" $ do
(code, _, _) <- readProcessWithExitCode "bash" ["test/Data/GraphQL/Test/Generation/run.sh", logFile] ""
case code of
ExitSuccess -> pure ()
ExitFailure _ -> do
output <- readFile logFile
assertFailure $ "===== Test failed. Output: =====\n" <> output
where
logFile = "/tmp/graphql-client-generation-test.log"
7 changes: 7 additions & 0 deletions graphql-client/test/Data/GraphQL/Test/Generation/codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
schema: schema.graphql
documents: queries/*.graphql

hsSourceDir: gen/
apiModule: Example.API
enumsModule: Example.Enums
scalarsModule: Example.Scalars
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query getUsers {
users {
id
name
age
gender
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query getAge($name: String!) {
userByName(name: $name) {
age
}
}

query getGender($name: String!) {
userByName(name: $name) {
gender
}
}
24 changes: 24 additions & 0 deletions graphql-client/test/Data/GraphQL/Test/Generation/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#
# Run the test, called in TestGeneration.hs

set -eux -o pipefail

builtin cd "$(dirname "${BASH_SOURCE}")"

LOG_FILE=$1
exec &> "${LOG_FILE}"

graphql-codegen

cat "${GHC_ENVIRONMENT}"

ghc \
-v \
--make \
-odir gen/out/ \
-hidir gen/out/ \
-isrc:gen \
Example.Scalars \
Example.Enums.Gender \
Example.API
20 changes: 20 additions & 0 deletions graphql-client/test/Data/GraphQL/Test/Generation/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type Query {
users: [User]
user(id: ID!): User
userByName(name: String!): User
}

type User {
id: ID!
name: String!
age: Age
gender: Gender
}

scalar Age

enum Gender {
MALE
FEMALE
OTHER
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Example.Scalars where

import Control.Monad (guard)
import Data.Aeson (FromJSON (..), ToJSON (..))

newtype Age = Age Int
deriving (Show)

instance FromJSON Age where
parseJSON v = do
n <- parseJSON v
guard $ n >= 0
pure $ Age n

instance ToJSON Age where
toJSON (Age n) = toJSON n
2 changes: 2 additions & 0 deletions graphql-client/test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Test.Tasty (defaultMain, testGroup)

import Data.GraphQL.Test.Generation.TestGeneration
import Data.GraphQL.Test.Monad.Class
import Data.GraphQL.Test.TestUtils

Expand All @@ -10,4 +11,5 @@ main =
"graphql-client"
[ testRunQuery
, testTestUtils
, testGeneration
]
Loading

0 comments on commit f67d1e1

Please sign in to comment.