github.com/mstephano/gqlgen-schemagen@v0.0.0-20230113041936-dd2cd4ea46aa/graphql/context_operation_test.go (about) 1 package graphql 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 "github.com/vektah/gqlparser/v2/ast" 9 ) 10 11 func TestGetOperationContext(t *testing.T) { 12 rc := &OperationContext{} 13 14 t.Run("with operation context", func(t *testing.T) { 15 ctx := WithOperationContext(context.Background(), rc) 16 17 require.True(t, HasOperationContext(ctx)) 18 require.Equal(t, rc, GetOperationContext(ctx)) 19 }) 20 21 t.Run("without operation context", func(t *testing.T) { 22 ctx := context.Background() 23 24 require.False(t, HasOperationContext(ctx)) 25 require.Panics(t, func() { 26 GetOperationContext(ctx) 27 }) 28 }) 29 } 30 31 func TestCollectAllFields(t *testing.T) { 32 t.Run("collect fields", func(t *testing.T) { 33 ctx := testContext(ast.SelectionSet{ 34 &ast.Field{ 35 Name: "field", 36 }, 37 }) 38 s := CollectAllFields(ctx) 39 require.Equal(t, []string{"field"}, s) 40 }) 41 42 t.Run("unique field names", func(t *testing.T) { 43 ctx := testContext(ast.SelectionSet{ 44 &ast.Field{ 45 Name: "field", 46 }, 47 &ast.Field{ 48 Name: "field", 49 Alias: "field alias", 50 }, 51 }) 52 s := CollectAllFields(ctx) 53 require.Equal(t, []string{"field"}, s) 54 }) 55 56 t.Run("collect fragments", func(t *testing.T) { 57 ctx := testContext(ast.SelectionSet{ 58 &ast.Field{ 59 Name: "fieldA", 60 }, 61 &ast.InlineFragment{ 62 TypeCondition: "ExampleTypeA", 63 SelectionSet: ast.SelectionSet{ 64 &ast.Field{ 65 Name: "fieldA", 66 }, 67 }, 68 }, 69 &ast.InlineFragment{ 70 TypeCondition: "ExampleTypeB", 71 SelectionSet: ast.SelectionSet{ 72 &ast.Field{ 73 Name: "fieldB", 74 }, 75 }, 76 }, 77 }) 78 s := CollectAllFields(ctx) 79 require.Equal(t, []string{"fieldA", "fieldB"}, s) 80 }) 81 82 t.Run("collect fragments with same field name on different types", func(t *testing.T) { 83 ctx := testContext(ast.SelectionSet{ 84 &ast.InlineFragment{ 85 TypeCondition: "ExampleTypeA", 86 SelectionSet: ast.SelectionSet{ 87 &ast.Field{ 88 Name: "fieldA", 89 ObjectDefinition: &ast.Definition{Name: "ExampleTypeA"}, 90 }, 91 }, 92 }, 93 &ast.InlineFragment{ 94 TypeCondition: "ExampleTypeB", 95 SelectionSet: ast.SelectionSet{ 96 &ast.Field{ 97 Name: "fieldA", 98 ObjectDefinition: &ast.Definition{Name: "ExampleTypeB"}, 99 }, 100 }, 101 }, 102 }) 103 resCtx := GetFieldContext(ctx) 104 collected := CollectFields(GetOperationContext(ctx), resCtx.Field.Selections, nil) 105 require.Len(t, collected, 2) 106 require.NotEqual(t, collected[0], collected[1]) 107 require.Equal(t, collected[0].Name, collected[1].Name) 108 }) 109 110 t.Run("collect fragments with same field name and different alias", func(t *testing.T) { 111 ctx := testContext(ast.SelectionSet{ 112 &ast.InlineFragment{ 113 TypeCondition: "ExampleTypeA", 114 SelectionSet: ast.SelectionSet{ 115 &ast.Field{ 116 Name: "fieldA", 117 Alias: "fieldA", 118 ObjectDefinition: &ast.Definition{Name: "ExampleTypeA"}, 119 }, 120 &ast.Field{ 121 Name: "fieldA", 122 Alias: "fieldA Alias", 123 ObjectDefinition: &ast.Definition{Name: "ExampleTypeA"}, 124 }, 125 }, 126 ObjectDefinition: &ast.Definition{Name: "ExampleType", Kind: ast.Interface}, 127 }, 128 }) 129 resCtx := GetFieldContext(ctx) 130 collected := CollectFields(GetOperationContext(ctx), resCtx.Field.Selections, nil) 131 require.Len(t, collected, 2) 132 require.NotEqual(t, collected[0], collected[1]) 133 require.Equal(t, collected[0].Name, collected[1].Name) 134 require.NotEqual(t, collected[0].Alias, collected[1].Alias) 135 }) 136 }