Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show ete test change with interface change #1

Open
wants to merge 4 commits into
base: include-interfaces-in-ete-test-schema
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,793 changes: 7,793 additions & 0 deletions ete_tests/index.html

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions ete_tests/src/EdgeCases/Interface/Character.elm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import Json.Decode as Decode


type alias Fragments decodesTo =
{ onHuman : SelectionSet decodesTo EdgeCases.Object.Human
{ onSpecies : SelectionSet decodesTo EdgeCases.Interface.Species
, onHuman : SelectionSet decodesTo EdgeCases.Object.Human
}


Expand All @@ -31,7 +32,8 @@ fragments :
-> SelectionSet decodesTo EdgeCases.Interface.Character
fragments selections____ =
Object.exhaustiveFragmentSelection
[ Object.buildFragment "Human" selections____.onHuman
[ Object.buildFragment "Species" selections____.onSpecies
, Object.buildFragment "Human" selections____.onHuman
]


Expand All @@ -40,7 +42,8 @@ update syntax to add `SelectionSet`s for the types you want to handle.
-}
maybeFragments : Fragments (Maybe decodesTo)
maybeFragments =
{ onHuman = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
{ onSpecies = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
, onHuman = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
}


Expand Down
12 changes: 6 additions & 6 deletions ete_tests/src/Normalize/Interface/Character.elm

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

12 changes: 6 additions & 6 deletions examples/src/Swapi/Interface/Character.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import Swapi.Union


type alias Fragments decodesTo =
{ onHuman : SelectionSet decodesTo Swapi.Object.Human
, onDroid : SelectionSet decodesTo Swapi.Object.Droid
{ onDroid : SelectionSet decodesTo Swapi.Object.Droid
, onHuman : SelectionSet decodesTo Swapi.Object.Human
}


Expand All @@ -33,8 +33,8 @@ fragments :
-> SelectionSet decodesTo Swapi.Interface.Character
fragments selections____ =
Object.exhaustiveFragmentSelection
[ Object.buildFragment "Human" selections____.onHuman
, Object.buildFragment "Droid" selections____.onDroid
[ Object.buildFragment "Droid" selections____.onDroid
, Object.buildFragment "Human" selections____.onHuman
]


Expand All @@ -43,8 +43,8 @@ update syntax to add `SelectionSet`s for the types you want to handle.
-}
maybeFragments : Fragments (Maybe decodesTo)
maybeFragments =
{ onHuman = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
, onDroid = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
{ onDroid = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
, onHuman = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
}


Expand Down
3 changes: 2 additions & 1 deletion generator/src/Graphql/Generator/Context.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Graphql.Generator.Context exposing (Context, stub)

import Dict exposing (Dict)
import Graphql.Parser.ClassCaseName as ClassCaseName exposing (ClassCaseName)
import Graphql.Parser.Type exposing (TypeDefinition)
import ModuleName exposing (ModuleName)


Expand Down Expand Up @@ -46,4 +47,4 @@ stub =


type alias InterfaceLookup =
Dict String (List ClassCaseName)
Dict String (List TypeDefinition)
63 changes: 39 additions & 24 deletions generator/src/Graphql/Generator/Group.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Graphql.Generator.Group exposing (IntrospectionData, generateFiles, sortedIntrospectionData)
module Graphql.Generator.Group exposing (IntrospectionData, generateFiles, interfaceImplementorsDict, sortedIntrospectionData)

import Dict exposing (Dict)
import Graphql.Generator.Context exposing (Context)
Expand All @@ -15,7 +15,7 @@ import Graphql.Generator.ScopeDefinitions as ScopeDefinitions
import Graphql.Generator.Subscription
import Graphql.Generator.Union
import Graphql.Generator.VerifyScalarCodecs
import Graphql.Parser.ClassCaseName as ClassCaseName exposing (ClassCaseName)
import Graphql.Parser.ClassCaseName as ClassCaseName
import Graphql.Parser.Type as Type exposing (TypeDefinition(..))
import ModuleName exposing (ModuleName)

Expand All @@ -38,22 +38,37 @@ sortedIntrospectionData typeDefinitions queryObjectName mutationObjectName subsc


typeDefName : TypeDefinition -> String
typeDefName (TypeDefinition name definableType description) =
typeDefName (TypeDefinition name _ _) =
ClassCaseName.normalized name


interfacePossibleTypesDict : List TypeDefinition -> Dict String (List ClassCaseName)
interfacePossibleTypesDict typeDefs =
typeDefs
|> List.filterMap
(\(TypeDefinition typeName definableType description) ->
case definableType of
Type.InterfaceType fields possibleTypes ->
Just ( ClassCaseName.raw typeName, possibleTypes )

_ ->
Nothing
)
interfaceImplementorsDict : List TypeDefinition -> Dict String (List TypeDefinition)
interfaceImplementorsDict typeDefs =
let
( interfaceTypes, objectTypes ) =
Tuple.pair typeDefs typeDefs
|> Tuple.mapBoth (List.filter Type.isInterfaceType) (List.filter Type.isObjectType)

interfaceImplementations : String -> List TypeDefinition -> List TypeDefinition
interfaceImplementations interfaceName objectsAndInterfaces =
-- Filter list based on Interfaces or Objects that implement the passed in Interface name
List.filter
(\objectOrInterface ->
Type.interfacesImplemented objectOrInterface
|> List.map ClassCaseName.raw
|> List.any ((==) interfaceName)
)
objectsAndInterfaces

interfaceToPossibleTypes : List ( String, List TypeDefinition )
interfaceToPossibleTypes =
List.map
(\interface ->
( Type.rawName interface, interfaceImplementations (Type.rawName interface) (List.append interfaceTypes objectTypes) )
)
interfaceTypes
in
interfaceToPossibleTypes
|> Dict.fromList


Expand All @@ -66,7 +81,7 @@ generateFiles options { typeDefinitions, queryObjectName, mutationObjectName, su
, mutation = mutationObjectName |> Maybe.map ClassCaseName.build
, subscription = subscriptionObjectName |> Maybe.map ClassCaseName.build
, apiSubmodule = options.apiSubmodule
, interfaces = interfacePossibleTypesDict typeDefinitions
, interfaces = interfaceImplementorsDict typeDefinitions
, scalarCodecsModule = options.scalarCodecsModule
}

Expand Down Expand Up @@ -101,23 +116,23 @@ excludeBuiltIns : List TypeDefinition -> List TypeDefinition
excludeBuiltIns typeDefinitions =
typeDefinitions
|> List.filter
(\(Type.TypeDefinition name definableType description) ->
(\(TypeDefinition name _ _) ->
not (ClassCaseName.isBuiltIn name)
)


excludeQuery : Context -> List TypeDefinition -> List TypeDefinition
excludeQuery { query } typeDefinitions =
typeDefinitions
|> List.filter (\(Type.TypeDefinition name definableType description) -> name /= query)
|> List.filter (\(TypeDefinition name _ _) -> name /= query)


excludeMutation : Context -> List TypeDefinition -> List TypeDefinition
excludeMutation { mutation } typeDefinitions =
case mutation of
Just mutationObjectName ->
typeDefinitions
|> List.filter (\(Type.TypeDefinition name definableType description) -> name /= mutationObjectName)
|> List.filter (\(TypeDefinition name _ _) -> name /= mutationObjectName)

Nothing ->
typeDefinitions
Expand All @@ -128,7 +143,7 @@ excludeSubscription { subscription } typeDefinitions =
case subscription of
Just subscriptionObjectName ->
typeDefinitions
|> List.filter (\(Type.TypeDefinition name definableType description) -> name /= subscriptionObjectName)
|> List.filter (\(TypeDefinition name _ _) -> name /= subscriptionObjectName)

Nothing ->
typeDefinitions
Expand All @@ -141,13 +156,13 @@ moduleToFileName modulePath =


toPair : Context -> TypeDefinition -> Maybe ( List String, String )
toPair context ((Type.TypeDefinition name definableType description) as definition) =
toPair context ((TypeDefinition name definableType description) as definition) =
let
moduleName =
ModuleName.generate context definition
in
(case definableType of
Type.ObjectType fields ->
Type.ObjectType fields _ ->
if name == context.query then
Graphql.Generator.Query.generate context moduleName fields
|> Just
Expand All @@ -171,15 +186,15 @@ toPair context ((Type.TypeDefinition name definableType description) as definiti
Graphql.Generator.Enum.generate name moduleName enumValues description
|> Just

Type.InterfaceType fields possibleTypes ->
Type.InterfaceType fields _ _ ->
Graphql.Generator.Interface.generate context (ClassCaseName.raw name) moduleName fields
|> Just

Type.UnionType possibleTypes ->
Graphql.Generator.Union.generate context name moduleName possibleTypes
|> Just

Type.InputObjectType fields ->
Type.InputObjectType _ ->
Nothing
)
|> Maybe.map (\fileContents -> ( moduleName, fileContents ))
34 changes: 26 additions & 8 deletions generator/src/Graphql/Generator/Interface.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Graphql.Generator.Field as FieldGenerator
import Graphql.Generator.Imports as Imports
import Graphql.Generator.ModuleName as ModuleName
import Graphql.Parser.ClassCaseName as ClassCaseName exposing (ClassCaseName)
import Graphql.Parser.Type as Type
import Graphql.Parser.Type as Type exposing (DefinableType(..), TypeDefinition(..))
import ModuleName
import String.Interpolate exposing (interpolate)

Expand All @@ -18,7 +18,7 @@ generate context name moduleName fields =
++ (List.map (FieldGenerator.generateForInterface context name) fields |> String.join "\n\n")


fragmentHelpers : Context -> List ClassCaseName -> List String -> String
fragmentHelpers : Context -> List TypeDefinition -> List String -> String
fragmentHelpers context implementors moduleName =
interpolate
"""
Expand Down Expand Up @@ -54,19 +54,37 @@ maybeFragments =
|> List.map (aliasFieldForFragment context moduleName)
|> String.join ",\n "
, implementors
|> List.map (exhaustiveBuildupForFragment context moduleName)
|> List.map (\(Type.TypeDefinition classCaseName _ _) -> exhaustiveBuildupForFragment context moduleName classCaseName)
|> String.join ",\n "
, implementors
|> List.map (maybeFragmentEntry context moduleName)
|> List.map (\(Type.TypeDefinition classCaseName _ _) -> maybeFragmentEntry context moduleName classCaseName)
|> String.join ",\n "
]


aliasFieldForFragment : Context -> List String -> ClassCaseName -> String
aliasFieldForFragment : Context -> List String -> TypeDefinition -> String
aliasFieldForFragment context moduleName interfaceImplementor =
interpolate
"on{0} : SelectionSet decodesTo {1}"
[ ClassCaseName.normalized interfaceImplementor, ModuleName.object context interfaceImplementor |> String.join "." ]
let
importPath : Maybe ( ClassCaseName, String )
importPath =
case interfaceImplementor of
TypeDefinition m (ObjectType _ _) _ ->
Just ( m, String.join "." <| ModuleName.object context m )

TypeDefinition m (InterfaceType _ _ _) _ ->
Just ( m, String.join "." <| ModuleName.interface context m )

_ ->
Nothing
in
Maybe.map
(\( m, path ) ->
interpolate
"on{0} : SelectionSet decodesTo {1}"
[ ClassCaseName.normalized m, path ]
)
importPath
|> Maybe.withDefault ""


exhaustiveBuildupForFragment : Context -> List String -> ClassCaseName -> String
Expand Down
6 changes: 3 additions & 3 deletions generator/src/Graphql/Generator/ModuleName.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Graphql.Parser.Type as Type exposing (TypeDefinition(..))


generate : Context -> TypeDefinition -> List String
generate context (Type.TypeDefinition name definableType description) =
generate context (TypeDefinition name definableType description) =
case definableType of
Type.ObjectType fields ->
Type.ObjectType fields interfaces ->
if name == context.query then
query context

Expand All @@ -27,7 +27,7 @@ generate context (Type.TypeDefinition name definableType description) =
Type.EnumType enumValues ->
enum context name

Type.InterfaceType fields possibleTypes ->
Type.InterfaceType fields possibleTypes interfaces ->
interface context name

Type.UnionType possibleTypes ->
Expand Down
4 changes: 2 additions & 2 deletions generator/src/Graphql/Generator/ScopeDefinitions.elm
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ generateType name =
objectName : TypeDefinition -> Bool
objectName (TypeDefinition name definableType description) =
case definableType of
Type.ObjectType _ ->
Type.ObjectType _ _ ->
True

_ ->
Expand All @@ -79,7 +79,7 @@ unionName (TypeDefinition name definableType description) =
interfaceName : TypeDefinition -> Bool
interfaceName (TypeDefinition name definableType description) =
case definableType of
Type.InterfaceType _ _ ->
Type.InterfaceType _ _ _ ->
True

_ ->
Expand Down
Loading