使用graphql获取特定对象时出错

时间:2017-12-12 07:43:48

标签: java graphql graphql-java

这是我的GraphQL Schema:

type Link {
  url: String!
  description: String!
}

type Query {
  allLinks: [Link]
  myLinks: [Link]
  link(url:String!):Link
}

type Mutation {
    createLink(url:String! , description:String!):Link
}

schema {
  query: Query
  mutation: Mutation
}

以下是用于查询的Java Pojo,Link& GraphQLRootResolver:

public class Link {
    private final String url;
    private final String description;

    public Link(String url, String description) {
        this.url = url;
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public String getDescription() {
        return description;
    }
}
public class Query implements GraphQLRootResolver {
    private final LinkRepository linkRepository;

    public Query(LinkRepository linkRepository) {
        this.linkRepository = linkRepository;
    }

    public List<Link> allLinks() {
        return linkRepository.getAllLinks();
    }

    public List<Link> myLinks() {
        return linkRepository.getAllLinks();
    }

    public Link link(String url) {
        return linkRepository.getLink(url);
    }
}

这是解析器的实现:

public List<Link> getAllLinks() {
    return links;
}

public void saveLink(Link link) {
    links.add(link);
}

public Link getLink(String str) {
    Link link = null;
    try {
        link = new Link(str, dashboardService.getDashboard(str));
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return link;
}

当我使用以下查询来获取特定网址的数据时,我收到以下错误:

http://localhost:8000/graphql?query={link(url:%22d_comp1%22){url}}

我得到的错误:

{"data":{"link":null},"errors":[{"message":"Internal Server Error(s) while executing query"}]}

由于我是GraphQL的新手,请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,因为我用来获取数据的查询是错误的。 正确的查询将是:

query={link(url:"d_comp_1"){url,description}}