Skip to content
Merged
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
71 changes: 51 additions & 20 deletions graphql/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,38 +464,69 @@ func newAdminResolverFactory() resolve.ResolverFactory {
}

func upsertEmptyGQLSchema() (*gqlSchema, error) {
varName := "GQLSchema"
existingSchemaVar := "ExistingGQLSchema"
xidInSchemaVar := "XidInSchema"
gqlType := "dgraph.graphql"

/*
query {
ExistingGQLSchema as ExistingGQLSchema(func: type(dgraph.graphql)) {
uid
dgraph.graphql.schema
XidInSchema as dgraph.graphql.xid
}
}
*/
qry := &gql.GraphQuery{
Attr: varName,
Var: varName,
Attr: existingSchemaVar,
Var: existingSchemaVar,
Func: &gql.Function{
Name: "eq",
Args: []gql.Arg{
{Value: gqlSchemaXidKey},
{Value: fmt.Sprintf("%q", gqlSchemaXidVal)},
},
Name: "type",
Args: []gql.Arg{{Value: gqlType}},
},
Filter: &gql.FilterTree{
Func: &gql.Function{
Name: "type",
Args: []gql.Arg{{Value: gqlType}},
},
Children: []*gql.GraphQuery{
{Attr: "uid"},
{Attr: gqlSchemaPred},
{Attr: gqlSchemaXidKey, Var: xidInSchemaVar},
},
Children: []*gql.GraphQuery{{Attr: "uid"}, {Attr: gqlSchemaPred}},
}

/*
mutation @if(eq(len(ExistingGQLSchema),1) AND eq(len(XidInSchema),0)) {
set {
"uid": "uid(ExistingGQLSchema)",
"dgraph.graphql.xid": "dgraph.graphql.schema"
}
}
mutation @if(eq(len(ExistingGQLSchema),0) AND eq(len(XidInSchema),0))
set {
"uid": "_:XidInSchema",
"dgraph.type": ["dgraph.graphql"],
"dgraph.graphql.xid": "dgraph.graphql.schema",
"dgraph.graphql.schema": ""
}
}
*/
mutations := []*dgoapi.Mutation{
{
SetJson: []byte(fmt.Sprintf(`
{
"uid": "uid(%s)",
"%s": "%s"
}`, existingSchemaVar, gqlSchemaXidKey, gqlSchemaXidVal)),
Cond: fmt.Sprintf(`@if(eq(len(%s),1) AND eq(len(%s),0))`, existingSchemaVar,
xidInSchemaVar),
},
{
SetJson: []byte(fmt.Sprintf(`
{
"uid": "_:%s",
"dgraph.type": ["%s"],
"%s": "%s",
"%s": ""
}`, varName, gqlType, gqlSchemaXidKey, gqlSchemaXidVal, gqlSchemaPred)),
Cond: fmt.Sprintf(`@if(eq(len(%s),0))`, varName),
}`, xidInSchemaVar, gqlType, gqlSchemaXidKey, gqlSchemaXidVal, gqlSchemaPred)),
Cond: fmt.Sprintf(`@if(eq(len(%s),0) AND eq(len(%s),0))`, existingSchemaVar,
xidInSchemaVar),
},
}

Expand All @@ -505,8 +536,8 @@ func upsertEmptyGQLSchema() (*gqlSchema, error) {
return nil, err
}

// the Alpha which created the gql schema node will get the uid here
uid, ok := resp.GetUids()[varName]
// the Alpha which created a new gql schema node with xid will get the uid here
uid, ok := resp.GetUids()[xidInSchemaVar]
if ok {
return &gqlSchema{ID: uid}, nil
}
Expand All @@ -516,8 +547,8 @@ func upsertEmptyGQLSchema() (*gqlSchema, error) {
return nil, schema.GQLWrapf(err, "Couldn't unmarshal response from Dgraph mutation")
}

// the Alphas which didn't create the gql schema node, will get the uid here.
gqlSchemaNode := result[varName].([]interface{})[0].(map[string]interface{})
// the Alphas which didn't create a new gql schema node, will get the uid here.
gqlSchemaNode := result[existingSchemaVar].([]interface{})[0].(map[string]interface{})
return &gqlSchema{
ID: gqlSchemaNode["uid"].(string),
Schema: gqlSchemaNode[gqlSchemaPred].(string),
Expand Down
16 changes: 16 additions & 0 deletions graphql/e2e/common/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ func admin(t *testing.T) {
addGQLSchema(t, client)
updateSchema(t, client)
updateSchemaThroughAdminSchemaEndpt(t, client)
gqlSchemaNodeHasXid(t, client)
}

func schemaIsInInitialState(t *testing.T, client *dgo.Dgraph) {
Expand Down Expand Up @@ -366,6 +367,21 @@ func updateSchemaThroughAdminSchemaEndpt(t *testing.T, client *dgo.Dgraph) {
introspect(t, adminSchemaEndptGQLSchema)
}

func gqlSchemaNodeHasXid(t *testing.T, client *dgo.Dgraph) {
resp, err := client.NewReadOnlyTxn().Query(context.Background(), `query {
gqlSchema(func: type(dgraph.graphql)) {
dgraph.graphql.xid
}
}`)
require.NoError(t, err)
// confirm that there is only one node of type dgraph.graphql and it has xid.
require.JSONEq(t, `{
"gqlSchema": [{
"dgraph.graphql.xid": "dgraph.graphql.schema"
}]
}`, string(resp.GetJson()))
}

func introspect(t *testing.T, expected string) {
queryParams := &GraphQLParams{
Query: `query {
Expand Down