Skip to content

Commit

Permalink
Merge pull request #133 from OxfordAbstracts/fixes-variable-types
Browse files Browse the repository at this point in the history
get var types from gql schema #131 and Add a single schema type #129
  • Loading branch information
roryc89 authored Sep 12, 2023
2 parents 0d0ced8 + 0f1d52e commit 981cac7
Show file tree
Hide file tree
Showing 42 changed files with 18,237 additions and 3,780 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

- uses: purescript-contrib/[email protected]
with:
purescript: "0.15.4"
purescript: "0.15.7"
psa: "0.8.2"
spago: "0.20.9"

Expand Down
2 changes: 1 addition & 1 deletion codegen/schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Argument for generateSchema
-- | External scalar types. The object key is the name of the scalar type in the graphQL schema.
-- | When the scalar type is encountered it will be set at the type in the provided module
-- | Useful for handling custom scalar types such as `Date`
, externalTypes ::
, gqlToPursTypes ::
Nullable
( Object
{ moduleName :: String
Expand Down
40 changes: 24 additions & 16 deletions e2e/1-affjax/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main =
queryGql "Widget names with id 1"
{ widgets: { id: 1 } =>> { name } }
logShow $ map _.name widgets
fullResult <-
fullResult <-
queryFullRes decodeJson identity client "Widget names with id 1"
{ widgets: { id: 1 } =>> { name } }

Expand All @@ -31,26 +31,34 @@ main =
logShow $ isJust fullResult.errors_json

-- Run gql query
queryGql ::
forall query returns.
GqlQuery Nil' OpQuery Schema query returns =>
DecodeJson returns =>
String -> query -> Aff returns
queryGql
:: forall query returns
. GqlQuery Nil' OpQuery Schema query returns
=> DecodeJson returns
=> String
-> query
-> Aff returns
queryGql = query client

client :: Client AffjaxNodeClient Nil' Schema Void Void
client
:: Client AffjaxNodeClient
{ directives :: Proxy Nil'
, query :: Schema
, mutation :: Void
, subscription :: Void
}
client = (Client $ AffjaxNodeClient "http://localhost:4000/graphql" [])

-- Schema
type Schema
= { prop :: String
, widgets :: { id :: Int } -> Array Widget
}

type Widget
= { name :: String
, id :: Int
}
type Schema =
{ prop :: String
, widgets :: { id :: Int } -> Array Widget
}

type Widget =
{ name :: String
, id :: Int
}

-- Symbols
prop :: Proxy "prop"
Expand Down
4 changes: 2 additions & 2 deletions e2e/2-comments/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/2-comments/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "3-comments",
"name": "2-comments",
"version": "1.0.0",
"description": "Handle comments in with code gen.",
"main": "server.js",
Expand Down
2 changes: 1 addition & 1 deletion examples/13-error-boundaries/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ queryGql name_ q = do
{ url: "http://localhost:4000/graphql"
, headers: []
}
queryFullRes decodeJson identity (client :: Client UrqlClient Nil' Schema _ _) name_ q
queryFullRes decodeJson identity (client :: Client UrqlClient { directives :: Proxy Nil', query :: Schema | _ }) name_ q

-- Schema
type Schema =
Expand Down
21 changes: 10 additions & 11 deletions examples/4-mutation/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class.Console (logShow)
import Generated.Gql.Schema.Admin (Mutation, Query, Schema)
import Generated.Gql.Schema.Admin.Enum.Colour (Colour(..))
import Generated.Gql.Schema.Admin (Query, Mutation)
import Generated.Gql.Symbols (colour, id)
import GraphQL.Client.Args (onlyArgs, (=>>))
import GraphQL.Client.BaseClients.Apollo (createClient, updateCacheJson)
Expand All @@ -18,7 +18,7 @@ import Type.Data.List (Nil')

main :: Effect Unit
main = do
client :: Client _ Nil' Query Mutation Void <-
client :: Client _ Schema <-
createClient
{ url: "http://localhost:4000/graphql"
, authToken: Nothing
Expand All @@ -34,12 +34,13 @@ main = do
logShow $ map _.colour widgets

{ set_widget_colour: affectedCount } <-
mutationOpts
_ { update = Just $ updateCacheJson client getWidgets \{ widgets: cacheWidgets } ->
{ widgets: cacheWidgets <#> \w -> w { colour = if w.id == Just 1 then GREEN else w.colour }
}
}
client
mutationOpts
_
{ update = Just $ updateCacheJson client getWidgets \{ widgets: cacheWidgets } ->
{ widgets: cacheWidgets <#> \w -> w { colour = if w.id == Just 1 then GREEN else w.colour }
}
}
client
"Update_widget_colour"
{ set_widget_colour: onlyArgs { id: 1, colour: GREEN }
}
Expand All @@ -54,10 +55,8 @@ main = do
logShow $ map _.colour updatedWidgets

{ widgets: updatedWidgetsWithoutCache } <-
queryOpts _ {fetchPolicy = Just NoCache } client "Widget_1_colour_no_cache" getWidgets
queryOpts _ { fetchPolicy = Just NoCache } client "Widget_1_colour_no_cache" getWidgets

-- Will also log [ GREEN ]
logShow $ map _.colour updatedWidgetsWithoutCache



5 changes: 2 additions & 3 deletions examples/5-subscription/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Aff (Milliseconds(..), delay, launchAff_)
import Effect.Class.Console (log, logShow)
import Generated.Gql.Schema.Admin (Query, Subscription, Mutation)
import Generated.Gql.Schema.Admin (Schema)
import GraphQL.Client.Args ((=>>))
import GraphQL.Client.BaseClients.Apollo (createSubscriptionClient)
import GraphQL.Client.Query (mutation)
import GraphQL.Client.Subscription (subscription)
import GraphQL.Client.Types (Client)
import Halogen.Subscription as HS
import Type.Data.List (Nil')

main :: Effect Unit
main = do
client :: Client _ Nil' Query Mutation Subscription <-
client :: Client _ Schema <-
createSubscriptionClient
{ url: "http://localhost:4000/graphql"
, authToken: Nothing
Expand Down
4 changes: 2 additions & 2 deletions examples/6-watch-query/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Aff (Milliseconds(..), delay, launchAff_)
import Effect.Class.Console (log, logShow)
import Generated.Gql.Schema.Admin (Query, Subscription, Mutation)
import Generated.Gql.Schema.Admin (Schema)
import GraphQL.Client.Args ((=>>))
import GraphQL.Client.BaseClients.Apollo (createSubscriptionClient, updateCacheJson)
import GraphQL.Client.Query (mutationOpts)
Expand All @@ -17,7 +17,7 @@ import Type.Data.List (Nil')

main :: Effect Unit
main = do
client :: Client _ Nil' Query Mutation Subscription <-
client :: Client _ Schema <-
createSubscriptionClient
{ url: "http://localhost:4000/graphql"
, authToken: Nothing
Expand Down
4 changes: 2 additions & 2 deletions examples/8-custom-gql-types/generate-purs-schema.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// In your code replace this line with the npm package:
// const { generateSchema } = require('purescript-graphql-client')
// import { generateSchema } = from 'purescript-graphql-client'
import { generateSchema } from '../../codegen/schema/index.mjs'

export default () =>
Expand All @@ -8,7 +8,7 @@ export default () =>
modulePath: ['Generated', 'Gql', 'Admin'],
useNewtypesForRecords: false,
url: 'http://localhost:4000/graphql',
gqlScalarsToPursTypes: {
gqlToPursTypes: {
GqlTypeThatIsAString: 'String',
GqlTypeThatIsAnInt: 'Int'
}
Expand Down
8 changes: 5 additions & 3 deletions examples/9-variables/server-fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (onListening) => {
const schema = buildSchema(`
type Query {
prop: String
widgets(colour: Colour): [Widget!]!
widgets(colour: Colour, ids: [Int!], ids_2: [Int]!): [Widget!]!
}
type Widget {
Expand All @@ -29,8 +29,10 @@ module.exports = (onListening) => {
prop: () => {
return 'Hello world!'
},
widgets: ({ colour }) =>
widgets.filter(w => !colour || colour === w.colour)
widgets: ({ colour, ids }) =>
widgets
.filter(w => !colour || colour === w.colour)
.filter(w => !ids || ids.includes(w.id))

}

Expand Down
45 changes: 32 additions & 13 deletions examples/9-variables/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import Data.Argonaut.Decode (class DecodeJson)
import Effect (Effect)
import Effect.Aff (Aff, launchAff_)
import Effect.Class.Console (logShow)
import Generated.Gql.Schema.Admin.Enum.Colour (Colour(..))
import Generated.Gql.Schema.Admin (Query)
import Generated.Gql.Symbols (colour)
import GraphQL.Client.Args ((=>>))
import Generated.Gql.Schema.Admin.Enum.Colour (Colour(..))
import Generated.Gql.Symbols (colour, widgets)
import GraphQL.Client.Alias ((:))
import GraphQL.Client.Args (NotNull, (=>>))
import GraphQL.Client.Operation (OpQuery)
import GraphQL.Client.Query (query_)
import GraphQL.Client.Types (class GqlQuery)
Expand All @@ -21,18 +22,36 @@ import Type.Proxy (Proxy(..))
main :: Effect Unit
main =
launchAff_ do
{ widgets } <-
queryGql "widget_colours_with_id1"
$ { widgets: { colour: Var :: _ "colourVar" Colour } =>> { colour } }
{ red_widgets, widgets } <-
queryGql "get_widgets"
$
{ red_widgets: widgets
:
{ colour: Var :: _ "colourVar" Colour
, ids_2: Var :: _ "ids_2" ((Array Int))
}
=>> { colour }
, widgets:
{ ids: Var :: _ "ids" (Array Int)
, ids_2: Var :: _ "ids_2" ((Array Int))
} =>> { id: unit }
}
`withVars`
{ colourVar: RED }
{ colourVar: RED
, ids: [ 1, 2 ]
, ids_2: [ 3, 4 ]
}
-- Will log [ RED ] as there is one red widget
logShow $ map _.colour widgets
logShow $ map _.colour red_widgets

logShow widgets

-- Run gql query
queryGql ::
forall query returns.
GqlQuery Nil' OpQuery Query query returns =>
DecodeJson returns =>
String -> query -> Aff returns
queryGql
:: forall query returns
. GqlQuery Nil' OpQuery Query query returns
=> DecodeJson returns
=> String
-> query
-> Aff returns
queryGql = query_ "http://localhost:4000/graphql" (Proxy :: Proxy Query)
46 changes: 23 additions & 23 deletions examples/9-variables/test.mjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { deepStrictEqual } from 'assert'
import execSh from 'exec-sh';
const {promise: exec} = execSh;
import { deepStrictEqual } from "assert";
import execSh from "exec-sh";
const { promise: exec } = execSh;

const logs = []
const logs = [];

console.log = (log) => {
console.info(log)
logs.push(log)
}
console.info(log);
logs.push(log);
};

import serverFn from './server-fn.js'
import gps from './generate-purs-schema.mjs'
import serverFn from "./server-fn.js";
import gps from "./generate-purs-schema.mjs";
serverFn(async () => {
try {
await gps()
await exec('npm run build', { stdio: 'pipe', stderr: 'pipe' })
const { main } = await import('./output/Main/index.js')
await gps();
await exec("npm run build", { stdio: "pipe", stderr: "pipe" });
const { main } = await import("./output/Main/index.js");

main()
main();
setTimeout(() => {
deepStrictEqual(logs, ['[RED]'])
console.info('tests passed')
process.exit(0)
}, 250)
deepStrictEqual(logs, ["[RED]", "[{ id: (Just 1) },{ id: (Just 2) }]"]);
console.info("tests passed");
process.exit(0);
}, 250);
} catch (err) {
console.error('test error', err)
process.exit(1)
console.error("test error", err);
process.exit(1);
}
})
});

setTimeout(() => {
console.error('Timeout')
process.exit(1)
}, 60000)
console.error("Timeout");
process.exit(1);
}, 60000);
Loading

0 comments on commit 981cac7

Please sign in to comment.