github.com/dgraph-io/dgraph@v1.2.8/graphql/e2e/common/schema.go (about)

     1  /*
     2   * Copyright 2019 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package common
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/dgraph-io/dgo/v2"
    24  	"github.com/dgraph-io/dgo/v2/protos/api"
    25  	"github.com/dgraph-io/dgraph/testutil"
    26  	"github.com/stretchr/testify/require"
    27  	"google.golang.org/grpc"
    28  )
    29  
    30  const (
    31  	expectedForInterface = `
    32  	{ "__type": {
    33          "name": "Employee",
    34  		"description": "GraphQL descriptions can be on interfaces.  They should work in the ` +
    35  		`input\nschema and should make their way into the generated schema.",
    36          "fields": [
    37              {
    38                  "name": "ename",
    39  				"description": ""
    40              }
    41  		],
    42  		"enumValues":[]
    43  	} }`
    44  
    45  	expectedForType = `
    46  	{ "__type": {
    47          "name": "Author",
    48  		"description": "GraphQL descriptions look like this.  They should work in the input\n` +
    49  		`schema and should make their way into the generated schema.",
    50          "fields": [
    51              {
    52  				"name": "id",
    53  				"description": ""
    54              },
    55              {
    56                  "name": "name",
    57  		"description": "GraphQL descriptions can be on fields.  They should work in the input\n` +
    58  		`schema and should make their way into the generated schema."
    59              },
    60              {
    61                  "name": "dob",
    62  				"description": ""
    63              },
    64              {
    65                  "name": "reputation",
    66  				"description": ""
    67              },
    68              {
    69                  "name": "country",
    70  				"description": ""
    71              },
    72              {
    73                  "name": "posts",
    74  				"description": ""
    75              }
    76  		],
    77  		"enumValues":[]
    78  	} }`
    79  
    80  	expectedForEnum = `
    81  	{ "__type": {
    82          "name": "PostType",
    83  		"description": "GraphQL descriptions can be on enums.  They should work in the input\n` +
    84  		`schema and should make their way into the generated schema.",
    85          "enumValues": [
    86              {
    87                  "name": "Fact",
    88  				"description": ""
    89              },
    90              {
    91              	"name": "Question",
    92  				"description": "GraphQL descriptions can be on enum values.  They should work in ` +
    93  		`the input\nschema and should make their way into the generated schema."
    94              },
    95              {
    96                  "name": "Opinion",
    97  				"description": ""
    98              }
    99  		],
   100  		"fields":[]
   101      } }`
   102  )
   103  
   104  func SchemaTest(t *testing.T, expectedDgraphSchema string) {
   105  	d, err := grpc.Dial(alphagRPC, grpc.WithInsecure())
   106  	require.NoError(t, err)
   107  
   108  	client := dgo.NewDgraphClient(api.NewDgraphClient(d))
   109  
   110  	resp, err := client.NewReadOnlyTxn().Query(context.Background(), "schema {}")
   111  	require.NoError(t, err)
   112  
   113  	testutil.CompareJSON(t, expectedDgraphSchema, string(resp.GetJson()))
   114  }
   115  
   116  func graphQLDescriptions(t *testing.T) {
   117  
   118  	testCases := map[string]struct {
   119  		typeName string
   120  		expected string
   121  	}{
   122  		"interface": {typeName: "Employee", expected: expectedForInterface},
   123  		"type":      {typeName: "Author", expected: expectedForType},
   124  		"enum":      {typeName: "PostType", expected: expectedForEnum},
   125  	}
   126  
   127  	query := `
   128  	query TestDescriptions($name: String!) {
   129  		__type(name: $name) {
   130  			name
   131  			description
   132  			fields {
   133  				name
   134  			  	description
   135  			}
   136  			enumValues {
   137  				name
   138  				description
   139  			}
   140  		}
   141  	}`
   142  
   143  	for testName, tCase := range testCases {
   144  		t.Run(testName, func(t *testing.T) {
   145  			introspect := &GraphQLParams{
   146  				Query: query,
   147  				Variables: map[string]interface{}{
   148  					"name": tCase.typeName,
   149  				},
   150  			}
   151  
   152  			introspectionResult := introspect.ExecuteAsPost(t, graphqlURL)
   153  			require.Nil(t, introspectionResult.Errors)
   154  
   155  			require.JSONEq(t, tCase.expected, string(introspectionResult.Data))
   156  		})
   157  	}
   158  }