github.com/dgraph-io/dgraph@v1.2.8/graphql/e2e/common/admin.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/stretchr/testify/require"
    26  	"google.golang.org/grpc"
    27  )
    28  
    29  const (
    30  	// Dgraph schema should look like this if the GraphQL layer has started and
    31  	// successfully connected
    32  	initSchema = `{
    33      "schema": [
    34          {
    35              "predicate": "dgraph.graphql.schema",
    36              "type": "string"
    37          },
    38          {
    39              "predicate": "dgraph.type",
    40              "type": "string",
    41              "index": true,
    42              "tokenizer": [
    43                  "exact"
    44              ],
    45              "list": true
    46          }
    47      ],
    48      "types": [
    49          {
    50              "fields": [
    51                  {
    52                      "name": "dgraph.graphql.schema"
    53                  }
    54              ],
    55              "name": "dgraph.graphql"
    56          }
    57      ]
    58  }`
    59  
    60  	firstTypes = `
    61  	type A {
    62  		b: String
    63  	}`
    64  	firstSchema = `{
    65      "schema": [
    66          {
    67              "predicate": "A.b",
    68              "type": "string"
    69          },
    70          {
    71              "predicate": "dgraph.graphql.schema",
    72              "type": "string"
    73          },
    74          {
    75              "predicate": "dgraph.type",
    76              "type": "string",
    77              "index": true,
    78              "tokenizer": [
    79                  "exact"
    80              ],
    81              "list": true
    82          }
    83      ],
    84      "types": [
    85          {
    86              "fields": [
    87                  {
    88                      "name": "A.b"
    89                  }
    90              ],
    91              "name": "A"
    92          },
    93          {
    94              "fields": [
    95                  {
    96                      "name": "dgraph.graphql.schema"
    97                  }
    98              ],
    99              "name": "dgraph.graphql"
   100          }
   101      ]
   102  }`
   103  	firstGQLSchema = `{
   104      "__type": {
   105          "name": "A",
   106          "fields": [
   107              {
   108                  "name": "b"
   109              }
   110          ]
   111      }
   112  }`
   113  
   114  	updatedTypes = `
   115  	type A {
   116  		b: String
   117  		c: Int
   118  	}`
   119  	updatedSchema = `{
   120      "schema": [
   121          {
   122              "predicate": "A.b",
   123              "type": "string"
   124          },
   125          {
   126              "predicate": "A.c",
   127              "type": "int"
   128          },
   129          {
   130              "predicate": "dgraph.graphql.schema",
   131              "type": "string"
   132          },
   133          {
   134              "predicate": "dgraph.type",
   135              "type": "string",
   136              "index": true,
   137              "tokenizer": [
   138                  "exact"
   139              ],
   140              "list": true
   141          }
   142      ],
   143      "types": [
   144          {
   145              "fields": [
   146                  {
   147                      "name": "A.b"
   148                  },
   149                  {
   150                      "name": "A.c"
   151                  }
   152              ],
   153              "name": "A"
   154          },
   155          {
   156              "fields": [
   157                  {
   158                      "name": "dgraph.graphql.schema"
   159                  }
   160              ],
   161              "name": "dgraph.graphql"
   162          }
   163      ]
   164  }`
   165  	updatedGQLSchema = `{
   166      "__type": {
   167          "name": "A",
   168          "fields": [
   169              {
   170                  "name": "b"
   171              },
   172              {
   173                  "name": "c"
   174              }
   175          ]
   176      }
   177  }`
   178  )
   179  
   180  func admin(t *testing.T) {
   181  	d, err := grpc.Dial(alphaAdminTestgRPC, grpc.WithInsecure())
   182  	require.NoError(t, err)
   183  
   184  	client := dgo.NewDgraphClient(api.NewDgraphClient(d))
   185  
   186  	err = checkGraphQLHealth(graphqlAdminTestAdminURL, []string{"NoGraphQLSchema"})
   187  	require.NoError(t, err)
   188  
   189  	schemaIsInInitialState(t, client)
   190  	addGQLSchema(t, client)
   191  	updateSchema(t, client)
   192  }
   193  
   194  func schemaIsInInitialState(t *testing.T, client *dgo.Dgraph) {
   195  	resp, err := client.NewReadOnlyTxn().Query(context.Background(), "schema {}")
   196  	require.NoError(t, err)
   197  
   198  	require.JSONEq(t, initSchema, string(resp.GetJson()))
   199  }
   200  
   201  func addGQLSchema(t *testing.T, client *dgo.Dgraph) {
   202  	err := addSchema(graphqlAdminTestAdminURL, firstTypes)
   203  	require.NoError(t, err)
   204  
   205  	resp, err := client.NewReadOnlyTxn().Query(context.Background(), "schema {}")
   206  	require.NoError(t, err)
   207  
   208  	require.JSONEq(t, firstSchema, string(resp.GetJson()))
   209  
   210  	introspect(t, firstGQLSchema)
   211  }
   212  
   213  func updateSchema(t *testing.T, client *dgo.Dgraph) {
   214  	err := addSchema(graphqlAdminTestAdminURL, updatedTypes)
   215  	require.NoError(t, err)
   216  
   217  	resp, err := client.NewReadOnlyTxn().Query(context.Background(), "schema {}")
   218  	require.NoError(t, err)
   219  
   220  	require.JSONEq(t, updatedSchema, string(resp.GetJson()))
   221  
   222  	introspect(t, updatedGQLSchema)
   223  }
   224  
   225  func introspect(t *testing.T, expected string) {
   226  	queryParams := &GraphQLParams{
   227  		Query: `query {
   228  			__type(name: "A") {
   229  				name
   230  				fields {
   231  					name
   232  				}
   233  			}
   234  		}`,
   235  	}
   236  
   237  	gqlResponse := queryParams.ExecuteAsPost(t, graphqlAdminTestURL)
   238  	requireNoGQLErrors(t, gqlResponse)
   239  
   240  	require.JSONEq(t, expected, string(gqlResponse.Data))
   241  }